While loop and do...while loop in C

🔄 While & Do-While Loops

Mastering Repetitive Execution in Programming

The while and do...while loops are used for repeated execution of a block of code as long as a specified condition is true.

🔁While Loop

The while loop is used when the number of iterations is not known beforehand, and it continues executing the block of code as long as the given condition is true.

Syntax
while (condition)
{
// Code to be executed in each iteration
}

💡Example:

#include <stdio.h>
int main() {
int i = 1;
 
// The loop continues as long as i is less than or equal to 5
while (i <= 5) {
printf("%d ", i); // Print the value of i
i++; // Increment i after each iteration
}
 
return 0;
}
Output: 1 2 3 4 5

In this example, the while loop checks the condition (i <= 5) before each iteration. The loop is entered if the condition is true, and the code inside the loop is executed. The loop continues until the condition becomes false.

Advantages

Flexible Initialization: The while loop provides flexibility in initializing the loop control variable before the loop starts. This allows you to set up the initial conditions based on complex logic.
Pre-test Loop: The while loop is a pre-test loop, meaning it checks the condition before entering the loop. This is beneficial in situations where you want to ensure the loop body is executed only if the condition is initially true.

⚠️Disadvantages

Potential for Infinite Loop: If the loop condition is not carefully managed, there is a risk of creating an infinite loop, causing the program to run indefinitely.

🔄do...while Loop

The do...while loop is similar to the while loop, but it guarantees that the block of code is executed at least once, as the condition is checked after the execution of the loop body.

Syntax
do
{
// Code to be executed in each iteration
} while (condition);

💡Example:

#include <stdio.h>
int main() {
int i = 1;
 
// The loop body is executed at least once
do {
printf("%d ", i); // Print the value of i
i++; // Increment i after each iteration
} while (i <= 5);
 
return 0;
}
Output: 1 2 3 4 5

In the do...while loop, the code inside the loop is executed first, and then the condition (i <= 5) is checked. If the condition is true, the loop continues; otherwise, it exits.

Advantages

Always Executes at Least Once: The do...while loop ensures that the loop body is executed at least once, regardless of the initial condition. This is useful when you want to guarantee the execution of certain statements before checking the loop condition.
Post-test Loop: Unlike the while loop, the do...while loop is a post-test loop. It checks the condition after executing the loop body. This can be advantageous in situations where you want to execute the loop body at least once, even if the condition is initially false.

⚠️Disadvantages

Limited Flexibility in Initialization: The do...while loop has less flexibility in initialization compared to the while loop because the loop body is executed before the condition is checked. This may limit the ability to perform complex setup logic before entering the loop.
Potential for Infinite Loop: Similar to the while loop, if the loop condition is not managed properly, there is a risk of creating an infinite loop.

🎯Key Takeaway

Both while and do...while loops are fundamental for controlling the flow of a program, especially when dealing with situations where the exact number of iterations is not known in advance.

Post a Comment

0 Comments