Iterative Structures
Iterative Structures
In programming, iteration or loop statements are used to execute a block of code repeatedly as
long as a certain condition is met. These loops help streamline repetitive tasks and make
programs more efficient.
Types of Loops:
Loop control variables are typically used to control the number of iterations and the progression
of the loop. These variables are usually initialized before the loop starts, and their values are
updated within the loop body.
▪ In a for loop, you initialize the loop control variable, specify the condition that determines
when the loop should continue, and specify how the control variable is updated.
▪ In a while and do-while loop, the initialization and update of loop control variables are done
outside the loop, and the condition is checked within the loop.
Loop Body:
The code enclosed within the loop is known as the loop body. This is the part that gets executed
repeatedly until the loop's condition becomes false.
Infinite Loops:
Be cautious when designing loops to avoid infinite loops, where the loop condition never becomes
false. Infinite loops can cause programs to hang or crash.
Loop Termination:
Loops terminate when their conditions become false. In a for loop, this often depends on the loop
control variable reaching a certain value or satisfying a condition. In a while or do-while loop, it
depends on the condition itself.
Break and Continue:
▪ The break statement is used to exit a loop prematurely, skipping the remaining iterations.
▪ The continue statement is used to skip the current iteration and move to the next iteration.
Nested Loops:
You can have loops inside other loops (nested loops) to implement complex iteration patterns.
Each loop should have its own loop control variable to avoid confusion.
Use Cases:
▪ Loops are commonly used for tasks such as iterating through arrays or collections of data.
▪ They are essential for implementing algorithms like sorting, searching, and repetitive
calculations.
▪ Loops are also used for creating interactive programs that keep running until the user
decides to exit.
Loop Efficiency:
Consider loop efficiency when dealing with large datasets. Minimize unnecessary calculations
within the loop body and use appropriate data structures and algorithms to improve performance.
The scope of loop control variables is typically limited to the loop in which they are declared. They
are not accessible outside the loop.
1. for Loop
• This is typically used when the number of iterations is known beforehand or when you
need to initialize and update a counter variable in a concise manner.
Syntax:
for (initialization; condition; update) {
// code to execute
}
• How It Works:
o Initialization: A variable is initialized before the loop starts.
o Condition: The loop continues as long as this condition is true.
o Update: The variable is updated after each iteration.
• Key Points:
o Combines initialization, condition, and update in one line.
o Best suited for counting loops or when the number of iterations is known.
Example:
for (int i = 0; i < 5; i++) {
printf(“%d “, i);
}
// Outputs: 0, 1, 2, 3, 4
2. while Loop
• Used when the number of iterations is not known beforehand, and the loop runs as long
as a condition remains true. The condition is checked before each iteration.
Syntax:
while (condition) {
// code to execute
}
• How It Works:
o The condition is checked before each iteration.
o If the condition is false initially, the loop will not execute.
• Key Points:
o The loop may not run at all if the condition is false from the start.
o Useful for condition-controlled loops, such as reading input or waiting for an
event.
Example:
#include <stdio.h>
int main() {
int i = 1; // Initialize a counter variable
while (i <= 5) { // Condition: Execute as long as i is less than or equal to 5
printf("%d ", i); // Print the value of i
i++; // Increment the counter variable
}
return 0;
}
3. do...while Loop
• The do...while loop is similar to the while loop, but the key difference is that the condition
is checked after each iteration. This means the code inside the loop runs at least once,
regardless of whether the condition is true.
Syntax:
do {
// code to execute
} while (condition);
• Here’s how it works:
o The code block within the do statement is executed.
o After the code block is executed, the condition is evaluated.
o If the condition is true (non-zero), the loop continues, and the code block is
executed again.
o This process repeats until the condition becomes false (zero).
o Once the condition becomes false, the loop terminates, and the program continues
with the code immediately following the loop.
• Key Points:
o The loop body is guaranteed to run at least once, regardless of the condition.
o Useful when you need user input or want to run code before checking a condition.
Example:
#include <stdio.h>
int main() {
int number;
do {
scanf("%d", &number);
return 0;
}
Key Points to Remember
• Initialization, Condition, and Update:
o for loop allows these three elements to be written in a single line, making it ideal
for counted loops.
o In while and do...while, you need to manage initialization and updates separately.
• Loop Execution Flow:
o for loop: Executes only when the condition is true and performs initialization and
updates in one line.
o while loop: Skips the loop if the condition is false from the start.
o do...while loop: Always runs at least once, even if the condition is initially false.
• Common Mistakes:
o Forgetting to update the loop counter, leading to infinite loops.
o Incorrect condition logic that prevents the loop from running.
Understanding these looping structures allows for more efficient and flexible control of program
flow, making them essential tools in problem-solving through iteration.
Comparison of Loops
Runs at
Loop Type Condition Check Best Use Case
least once
for loop Before each iteration No When the number of iterations is known