Panel For Example Panel For Example Panel For Example

C Loop Structures and Examples

Author : Adrian January 16, 2026

 

Overview

This article describes the syntax and usage of C language while, do-while, and for loop structures, as well as loop exit statements.

 

1. while loop

When a task must be repeated while a condition is true, a while loop can be used. The expression is evaluated before executing the loop body; if the expression is false initially, the loop body is not executed at all. The loop body can be an empty statement, a single statement, or a block enclosed in { }.

General syntax:

while(expression) statement;

Example: Output multiplication table for 1–5 times 10 (inefficient version)

manifest sample.c #include int main(){ int result1,result2,result3,result4,result5; result1 = 1 * 10; printf("1 * 10 = %dn",result1); result2 = 2 * 10; printf("2 * 10 = %dn",result2); result3 = 3 * 10; printf("3 * 10 = %dn",result3); result4 = 4 * 10; printf("4 * 10 = %dn",result4); result5 = 5 * 10; printf("5 * 10 = %dn",result5); }

The while loop is useful to avoid repeating nearly identical statements. A typical while loop iterates until the loop condition becomes false. Note that the loop variable(s) used in the condition must be updated inside the loop; otherwise the loop becomes an infinite loop.

Example: Output 1–10 times 10 using while

manifest sample.c #include int main(){ // Declare variable num, initial value 1 int num = 1; // Declare variable result to store the calculation result int result; while( num <= 10 ){ result = num * 10; printf(" %d * 10 = %d n", num, result); // Use increment operator to modify loop variable num++; }}

The num++ statement modifies the loop condition so that the loop will eventually terminate when num becomes 11. If the loop variable is not updated, the loop will not exit and will be an infinite loop.

2. do-while loop

A do-while loop executes the loop body first, then evaluates the condition. Therefore the loop body is executed at least once. When multiple statements participate in the loop body, enclose them in { }.

General syntax:

do{ statement; }while(condition);

Note: A semicolon is required after the while(condition).

 

3. Nested loops

Loops can be nested one inside another. The outer loop triggers the inner loop; the inner loop runs to completion before the outer loop proceeds to its next iteration.

Example 1: Nested while to print a right triangle

Task: Print a right triangle shape using '*'.

manifest sample.c #include void main(){ int nstars = 1, stars; // Outer loop controls number of output lines while( nstars <= 10 ){ stars = 1; // Inner loop controls number of stars printed while( stars <= nstars ) { printf("*"); stars++; } printf("n"); nstars++; }}

 

4. for loop

The for loop is widely used and flexible. It can replace while in many cases, and it supports initialization, condition, and update expressions.

General syntax:

for(expression1; expression2; expression3){ statement; }

Behavior:

  1. expression1 runs once before the first iteration (commonly initialization).
  2. expression2 is evaluated before each iteration; if true, the loop body runs; if false, the loop exits.
  3. expression3 runs after each iteration (commonly updating the loop variable), then control returns to step 2.

 

Omitting for expressions

Expression1 can be omitted if initialization occurs before the loop. Expression2 can be omitted if a break condition is implemented inside the loop body. Expression3 can be omitted if the loop body updates the loop variable. All three can be omitted, but then the loop body must manage updates and termination, or the loop may become infinite.

int i = 0; for( ;; ){ i = i + 1; if( i >= 10 ) break; printf("n %d=", i*2); }

 

5. Loop exit: break and continue

To exit a loop early, use break. To skip the remainder of the current iteration and continue with the next iteration, use continue.

break: exits the smallest enclosing loop or switch and transfers control to the statement after that construct.

Example: break

manifest sample.c #include void main(){ int i = 0; while(i < 7){ i++; printf("n The %d cycle", i); if(i == 5){ printf("n i==5 Exit the loop and the program ends", i); break; } }}

When i equals 5, the break statement terminates the loop.

continue: used only inside loops. For while and do-while loops, continue transfers control to the condition check. For for loops, continue transfers control to the update expression.

The difference: break terminates the loop, while continue skips the rest of the current iteration and proceeds with the next iteration.

 

Example: continue

Task: Sum integers from 1 to 100 but skip numbers whose ones digit is 3.

manifest sample.c #include void main(){ int i; int sum = 0; for( i = 1; i <= 100; i++ ){ if( i % 10 == 3 ){ continue; } sum += i; } printf("sum = %d", sum); }

The program checks i % 10 == 3; when true, continue skips adding that number.

 

6. for nested loops

for loops can be nested with other for or while loops.

Example: Print a diamond pattern using nested for loops

Task: Print a diamond made of '*'.

diamond pattern example

manifest sample.c #include void main(){ int i, j, k; // Print the top four rows for (i = 1; i <= 4; i++) { // Control the number of spaces to print for (j = 1; j <= 4 - i; j++) printf(" "); // Control the number of stars to print for (k = 1; k <= 2 * i - 1; k++) printf("*"); printf("n"); } // Print the bottom three rows for (i = 1; i <= 3; i++) { // Control the number of spaces to print for (j = 1; j <= i; j++) printf(" "); // Control the number of stars to print for (k = 1; k <= 7 - 2 * i; k++) printf("*"); printf("n"); }}

Example: Print prime numbers between 100 and 200

Task: Print all prime numbers between 100 and 200. A prime number is divisible only by 1 and itself. The algorithm checks divisibility starting from 2 upward.

manifest sample.c #include void main(){ int i, j, n; n = 0; for (i = 100; i <= 200; i++) { j = 2; // Find the first divisor between 2 and i while (i % j != 0) j++; // If the first divisor equals the number itself, it is prime if (i == j) { printf("%4d", i); n++; // Print 8 primes per line if (n % 8 == 0) printf("n"); } } printf("n"); }

This program uses an outer for loop over the candidate numbers and an inner while loop to find the first divisor. If the first divisor equals the candidate number, it is prime.