Control Flow
Control flow refers to the order in which statements are executed in a program. By default, programs execute statements sequentially, from top to bottom. However, programming languages provide constructs that allow you to alter this default flow, enabling more complex and dynamic behavior. These constructs include conditional statements and loops.
Conditional Statements
Conditional statements allow your program to make decisions based on whether certain conditions are true or false. The most common conditional statement is the if
statement.
The if
Statement
The if
statement executes a block of code only if a specified condition evaluates to true.
if (condition) {
// code to execute if condition is true
}
The if...else
Statement
The if...else
statement executes one block of code if the condition is true, and another block of code if the condition is false.
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
The if...else if...else
Statement
This structure allows you to check multiple conditions in sequence. The first condition that evaluates to true will have its corresponding code block executed.
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if no conditions are true
}
The switch
Statement
The switch
statement is a more concise way to select one of many code blocks to be executed. It's particularly useful when you have a single variable or expression to check against multiple possible constant values.
Example: Using switch
let day = "Monday";
switch (day) {
case "Monday":
console.log("It's the start of the week.");
break;
case "Friday":
console.log("It's almost the weekend!");
break;
default:
console.log("Just another day.");
}
The break
statement is crucial in a switch
statement; it prevents the execution from falling through to the next case.
Loops
Loops allow you to execute a block of code repeatedly. This is essential for processing collections of data or performing repetitive tasks.
The for
Loop
The for
loop is typically used when you know in advance how many times you want to iterate.
for (initialization; condition; afterthought) {
// code to execute in each iteration
}
Initialization: Executed once before the loop starts (e.g., setting a counter to 0).
Condition: Evaluated before each iteration. If true, the loop continues; if false, the loop terminates.
Afterthought: Executed at the end of each iteration (e.g., incrementing the counter).
The while
Loop
The while
loop executes a block of code as long as a specified condition is true. The condition is checked at the beginning of each iteration.
while (condition) {
// code to execute as long as condition is true
// make sure to update variables that affect the condition to avoid infinite loops
}
The do...while
Loop
The do...while
loop is similar to the while
loop, but it executes the code block *at least once* before checking the condition. This means the code inside the do
block will always run, even if the condition is initially false.
do {
// code to execute at least once
// then check condition
} while (condition);
Loop Control Statements
You can also use special statements to alter the behavior of loops:
break
: Exits the current loop immediately.continue
: Skips the rest of the current iteration and proceeds to the next iteration of the loop.