In programming, you often need to execute a block of code multiple times. Writing the same code over and over again is inefficient and prone to errors. This is where loops come in.
Loops allow you to execute a specific block of code repeatedly as long as a specified condition remains true. The while loop is one of the most fundamental looping structures in the C programming language.
while Loop WorksThe while loop checks a condition before it executes the code block. If the condition evaluates to true (a non-zero value), the code inside the loop runs. After the block runs, the condition is checked again. This process repeats until the condition becomes false (evaluates to 0).
Syntax:
while (condition) {
// Code to be executed
// Make sure to update a variable here so the condition eventually becomes false!
}
#include <stdio.h>int main() { int i = 1; // 1. Initialization
// 2. Condition check while (i <= 5) { printf("The value of i is: %d\n", i); // 3. Increment/Update (Crucial step!) i++; }
printf("Loop finished!\n"); return 0; }
int i = 1;).i <= 5).i++).An infinite loop is a loop that never stops running. This happens when the loop condition never evaluates to false. In most cases, this is a bug caused by forgetting to update the counter variable.
An infinite loop will freeze your program and consume CPU resources until the program is forcefully terminated.
#include <stdio.h>int main() { int x = 1;
while (x < 5) { printf("This will print forever!\n"); // We forgot x++ here! x will ALWAYS be 1. }
return 0; }
Pro Tip: Always double-check that your
whileloop has a clear path to finishing. Verify that your variables are incrementing or decrementing correctly.
do...while LoopThe do...while loop is a variant of the standard while loop. The key difference is that a do...while loop checks its condition at the bottom of the loop, not the top.
This guarantees that the code inside the loop will execute at least once, regardless of whether the condition is true or false initially.
Syntax:
do {
// Code to be executed
} while (condition);
do...while Example#include <stdio.h>int main() { int i = 10;
// Even though 10 is NOT less than 5, this will run once! do { printf("i is %d\n", i); i++; } while (i < 5);
return 0; }
When to use do...while: It is extremely useful when taking user input. You want to prompt the user at least once, and then keep asking while the input is invalid.
Let's use a while loop to write a program that calculates the sum of all positive integers entered by a user until they enter a negative number or zero.
#include <stdio.h>int main() { int number = 1; // Initialized to a positive value to enter the loop int sum = 0;
printf("Enter numbers to add (enter 0 or negative to stop):\n");
while (number > 0) { scanf("%d", &number); if (number > 0) { sum = sum + number; number--; } }
printf("The total sum is: %d\n", sum); return 0; }
To write clean, robust C code and rank well in technical assessments, consider these tips:
while condition becomes too complex, extract it into a separate variable or function for readability.stdbool.h: For modern C programming, using while (true) with an internal break statement can sometimes be cleaner than a complicated loop expression.while (i < 5) runs 5 times if i starts at 0, but 4 times if i starts at 1.What guarantees that a loop's block of code will execute at least once?
What is the most common cause of an infinite loop?