What is Program Flow?
Program flow is the order in which a computer executes the instructions in your code. By default, a program runs sequentially, line by line, from top to bottom. However, to make programs useful, we often need to:
Make decisions: Execute certain code only if specific conditions are met.
Repeat actions: Run a block of code multiple times.
Organize code: Group tasks into reusable pieces.
To achieve this, we use conditional statements, loops, and functions. Let’s break each of these down.
Conditional Statements
Conditional statements let your program make decisions by executing different code blocks based on whether a condition is true or false.
If Statement
The if statement is the simplest way to add decision-making to your code. It checks a condition an expression that evaluates to true or false and runs a block of code only if the condition is true.
let temperature = 25; if (temperature > 20) { console.log("It's a warm day!"); }
Else Statement
The else statement pairs with if to handle the case when the condition is false. It provides an alternative action.
let age = 16; if (age >= 18) { console.log("You can vote."); } else { console.log("You’re too young to vote."); }
Switch Statement
The switch statement is useful when you need to check a single value against multiple possibilities. It’s cleaner than writing many if-else statements for the same variable.
switch (expression) {
case value1:
// Code to run if expression equals value1
break;
case value2:
// Code to run if expression equals value2
break;
default:
// Code to run if no cases match
}
Expression: The value you’re testing (e.g., a variable).
Case: A possible value the expression might match.
Break: Stops the switch from running all cases after a match.
Default: Runs if no case matches (like an else).
Here’s an example:
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
Loops
Loops let you repeat a block of code multiple times. Below are the types of loops.
For Loop
The for loop is perfect when you know exactly how many times you want to repeat something. It has three parts inside its parentheses: initialization, condition, and update.
for (initialization; condition; update) {
// Code to repeat
}
Initialization: Sets up a variable (e.g., let i = 0).
Condition: Checked before each loop; if true, the code runs.
Update: Changes the variable after each loop (e.g., i++ adds 1).
- While Loop
The while loop repeats code as long as a condition is true. It’s useful when you don’t know how many times you’ll need to loop ahead of time.
while (condition) {
// Code to repeat
}
let count = 0;
while (count < 3) {
console.log(count);
count++;
}
Do-While Loop
The do-while loop is like while, but it runs the code at least once before checking the condition.
let number = 0;
do {
console.log(number);
number++;
} while (number < 3);
Use do-while when you need the code to execute at least once, regardless of the condition.
Break Statement
The break statement immediately stops a loop and exits it, skipping any remaining iterations. It’s like hitting the emergency stop button—useful when you’ve found what you need or want to avoid unnecessary work.
for (initialization; condition; update) {
if (someCondition) {
break; // Exit the loop right away
}
// More code
}
for (let i = 0; i < 5; i++) {
if (i === 3) {
break; // Stop when i is 3
}
console.log("Number:", i);
}
Continue Statement
The continue statement skips the current iteration of a loop and moves to the next one. Think of it as saying, Skip this round, but keep going. It’s handy when you want to bypass certain cases without stopping the entire loop.
for (initialization; condition; update) {
if (someCondition) {
continue; // Skip to the next iteration
}
// More code
}
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // Skip when i is 2
}
console.log(i);
}
Functions
Functions are reusable blocks of code that perform a specific task.
Function Declaration
A function is defined with the function keyword, a name, and a code block. You can call (run) it whenever needed.
Syntax:
function functionName() {
// Code to execute
}
Example:
function sayHello() {
console.log("Hello, world!");
}
sayHello(); // Runs the function
Parameters
Parameters are variables you pass into a function to customize its behavior. When you call the function, you provide arguments (actual values) for those parameters.
Syntax:
function functionName(parameter1, parameter2) {
// Use parameters in the code
}
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Darshan"); // Argument: "Darshan"
Return Statement
The return statement lets a function produce an output. After return runs, the function stops executing.
Syntax:
function functionName() {
return value; // Output of the function
}
function add(a, b) {
return a + b;
}
let sum = add(5, 3);
console.log(sum); // Output: 8
Conclusion:
Program flow refers to the sequence in which code instructions are executed. This article covers key concepts that control program flow: conditional statements, such as if, else, and switch, guide decision-making; loops, including for, while, and do-while, facilitate repeated actions; and functions enable code organization and reuse. Understanding these tools is essential for creating efficient, dynamic programs