C language content

In C programming, loops are used to execute a block of code repeatedly as long as a specified condition is met. C provides three types of loops: for, while, and do-while. Each loop type is useful in different scenarios and has its own syntax and behavior. Here’s a detailed explanation of each loop type:

For Loop

The for loop is used when you know in advance how many times you want to execute a statement or a block of statements. The syntax of a for loop is:

				
					for (initialization; condition; increment) {
    // Code to be executed
}

				
			
  • Initialization: It is executed once at the beginning of the loop. It’s typically used to initialize a counter variable.
  • Condition: This is evaluated before each iteration. If the condition is true, the loop continues; if it’s false, the loop stops.
  • Increment: This is executed after each iteration of the loop. It’s typically used to update the counter variable.

Example:

				
					#include <stdio.h>

int main() {
    int i;
    for (i = 0; i < 5; i++) {
        printf("i = %d\n", i);
    }
    return 0;
}

				
			

In this example, the loop starts with i = 0 and increments i by 1 in each iteration until i becomes 5. The output will be:

				
					i = 0
i = 1
i = 2
i = 3
i = 4

				
			

More For loop Example: (Five table)

				
					#include <stdio.h>

int main() {
    int i;
    int number = 5;  // the number for which we want to print the table

    // Loop to print the table of 5
    for (i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", number, i, number * i);
    }

    return 0;
}

				
			

Out Put

				
					5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

				
			

While Loop

The while loop is used when you want to execute a block of code repeatedly as long as a condition is true. The syntax of a while loop is:

				
					while (condition) {
    // Code to be executed
}

				
			

Condition: This is evaluated before each iteration. If the condition is true, the loop continues; if it’s false, the loop stops.

Example:

				
					#include <stdio.h>

int main() {
    int i = 0;
    while (i < 5) {
        printf("i = %d\n", i);
        i++;
    }
    return 0;
}

				
			

In this example, the loop starts with i = 0 and increments i by 1 in each iteration until i becomes 5. The output will be:

				
					i = 0
i = 1
i = 2
i = 3
i = 4

				
			

More while loop Example: (Five table)

				
					#include <stdio.h>

int main() {
    int i = 1;  // initialize loop counter
    int number = 5;  // the number for which we want to print the table

    // Loop to print the table of 5
    while (i <= 10) {
        printf("%d x %d = %d\n", number, i, number * i);
        i++;  // increment the loop counter
    }

    return 0;
}

				
			

Out Put

				
					5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

				
			

Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the code block will be executed at least once, because the condition is checked after the block is executed. The syntax of a do-while loop is:

				
					do {
    // Code to be executed
} while (condition);

				
			
  • Condition: This is evaluated after each iteration. If the condition is true, the loop continues; if it’s false, the loop stops.

Example:

				
					#include <stdio.h>

int main() {
    int i = 0;
    do {
        printf("i = %d\n", i);
        i++;
    } while (i < 5);
    return 0;
}

				
			

In this example, the loop starts with i = 0 and increments i by 1 in each iteration until i becomes 5. The output will be:

				
					i = 0
i = 1
i = 2
i = 3
i = 4

				
			

More do-while loop Example: (Five table)

				
					#include <stdio.h>

int main() {
    int i = 1;  // initialize loop counter
    int number = 5;  // the number for which we want to print the table

    // Loop to print the table of 5
    do {
        printf("%d x %d = %d\n", number, i, number * i);
        i++;  // increment the loop counter
    } while (i <= 10);

    return 0;
}

				
			

Out Put

				
					5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

				
			

Comparison

  1. Initialization: The for loop allows you to initialize a variable as part of the loop syntax. In while and do-while, initialization must be done separately before the loop.
  2. Condition Check: The for and while loops check the condition before the first iteration (pre-test loops). The do-while loop checks the condition after the first iteration (post-test loop), ensuring the loop body executes at least once.
  3. Increment/Decrement: In the for loop, increment/decrement is part of the loop syntax. In while and do-while loops, it must be done inside the loop body.

Practical Use Cases

  • For Loop: Best used when the number of iterations is known beforehand (e.g., iterating through arrays).
  • While Loop: Suitable when the number of iterations is not known and depends on a condition being met (e.g., reading input until a specific value is entered).
  • Do-While Loop: Useful when the loop body must execute at least once regardless of the condition (e.g., menu-driven programs that should display at least once).

By understanding these loops and their differences, you can choose the most appropriate one for your specific problem in C programming.

Scroll to Top