Mastering Program Flow: Conditional and Iteration Structures
Classified in Computers
Written on in
English with a size of 4.62 KB
Conditional Control Structures
Conditional control structures dictate the flow of execution by allowing specific blocks of code to run only when certain conditions are met.
1. The If Statement
The If statement executes a block of code only if the specified condition evaluates to true. Otherwise, the block is skipped entirely.
Syntax:
if (condition) {
// statements (If Block)
}
// other statements
Note: A flow chart typically illustrates the execution path based on the condition.
2. The If-Else Statement
If the condition is true, the If block is executed. If the condition is false, the alternative Else block is executed.
Syntax:
if (condition) {
// statements (If Block)
} else {
// statements (Else Block)
}
3. The Else-If Ladder Statement
The Else-If ladder allows checking multiple conditions sequentially. If a condition is true, its corresponding block is executed, and the rest of the ladder is skipped. If all conditions are false, the final Else block (default statement) is executed.
Syntax:
if (condition1) {
// statement 1 (If Block)
} else if (condition2) {
// statement 2 (Else-If block 1)
} else if (condition3) {
// statement 3 (Else-If block 2)
}
// ... additional else if blocks ...
else {
// default statement (Else Block)
}
// Statement x (executed after the ladder)
4. Nested If-Else Statements
A Nested If-Else statement occurs when an If or If-Else structure is placed inside another If or Else block. This allows for complex, multi-level decision making.
Syntax:
if (condition1) {
if (condition2) {
statement 1;
} else {
statement 2;
}
} else {
if (condition3) {
statement 3;
} else {
statement 4;
}
}
// Statement x (executed after the main structure)
Loop Control Structures (Iteration)
Loop control structures, or looping statements, are used to execute a block of code repeatedly based on a specified condition. The statements continue to execute as long as the condition remains true. The iteration stops when the condition becomes false.
There are three primary types of looping statements:
- For statements
- While statements
- Do-While statements
1. The For Statement
The For statement is a compact looping structure where the initialization, the termination condition, and the increment/decrement operation are all defined on the same line.
Syntax:
for (initialization; condition; increment/decrement) {
statement;
}
Example Iteration Flow:
| Iteration | Condition (e.g., i <= 5) | Output | Result |
|---|---|---|---|
| i=1 | 1 <= 5 (true) | "hello" is printed | Continues loop |
| i=2 | 2 <= 5 (true) | "hello" is printed | Continues loop |
| i=3 | 3 <= 5 (true) | "hello" is printed | Continues loop |
| i=4 | 4 <= 5 (true) | "hello" is printed | Continues loop |
| i=5 | 5 <= 5 (true) | "hello" is printed | Continues loop |
| i=6 | 6 <= 5 (false) | - | For loop is terminated |
2. The While Statement
The While statement is a pre-tested loop. Unlike the For loop, the initialization, condition check, and increment/decrement steps are typically written in separate locations, offering more flexibility.
Syntax:
initialization;
while (condition) {
statement;
increment/decrement;
}
3. The Do-While Statement
The Do-While statement is fundamentally different from the For and While loops because it is a post-tested loop. The condition is tested at the end of the loop execution.
Crucially, a Do-While loop is guaranteed to execute at least once, even if the condition is initially false, because the condition check is bypassed during the first iteration.
Syntax:
initialization;
do {
statement;
increment/decrement;
} while (condition); // Note the semicolon