#5 Loops in C Programming - for loop, while loop and do-while loop

Categories - C Programming Tags - Programming   Maniruzzaman Akash   1 month ago   217   4 minutes   0

Loops are an essential feature of many programming languages, including C. They allow developers to execute a block of code repeatedly, making it easy to perform a task multiple times without having to write the same code over and over again.

There are several different types of loops in C programming, each with its own syntax and usage. The most common loops are the “while” loop, the “do-while” loop, and the “for” loop.

while loop

The “while” loop is the simplest loop in C.

It has the following syntax:

while (expression)
{
    // code to be executed
}

The “while” loop continues to execute the code block as long as the expression is true. For example, consider a program that counts down from 10 to 1:

int i = 10;
while (i > 0)
{
    printf("%d\n", i);
    i--;
}

In this program, the “while” loop executes the code block as long as the value of the variable “i” is greater than 0. Each time the loop runs, it prints the current value of “i” and then decrements “i” by 1. The output of this program would be the numbers 10 to 1 on separate lines.

do-while loop

The “do-while” loop is similar to the “while” loop, but it executes the code block at least once before checking the expression. Its syntax is as follows:

Copy code do { // code to be executed } while (expression); The “do-while” loop is often used when the code block must be executed at least once, regardless of the value of the expression. For example, consider a program that prompts the user to enter a number and then prints the entered number:

int num;
do
{
    printf("Enter a number: ");
    scanf("%d", &num);
}
while (num == 0);
printf("You entered: %d\n", num);

In this program, the “do-while” loop executes the code block at least once, regardless of whether the user enters a valid number. If the user enters 0, the loop will run again, prompting the user to enter a different number. If the user enters a non-zero number, the loop will end and the program will print the entered number.

for loop

The “for” loop is the most versatile and powerful loop in C programming. It allows developers to control the initialization, condition, and increment of a loop in a single statement. The syntax for a “for” loop in C is as follows:

for (initialization; expression; increment)
{
    // code to be executed
}

The “initialization” part of the “for” loop is executed once at the beginning of the loop, and is typically used to declare and initialize a loop counter. The “expression” is evaluated before each iteration of the loop, and the loop continues to run as long as the expression is true. The “increment” is executed at the end of each iteration of the loop, and is typically used to update the loop counter.

Counting down from 10 to 1:

for (int i = 10; i > 0; i--)
{
    printf("%d\n", i);
}

In this “for” loop, the “initialization” part declares and initializes the loop counter “i” to 10. The “expression” checks whether “i” is greater than 0, and the “increment” decrements “i” by 1 after each iteration of the loop. The code block inside the “for” loop simply prints the current value of “i”. The output of this program would be the numbers 10 to 1 on separate lines.

1 Example in 3 loop - pseudo-code

Here are three programs in C that use a “while” loop, a “for” loop, and a “do-while” loop to sum the numbers from 1 to 10:

Using a “while” loop:

int sum = 0;
int i = 1;
while (i <= 10)
{
    sum += i;
    i++;
}
printf("The sum of the numbers from 1 to 10 is %d\n", sum);

In this program, the “while” loop continues to run as long as the value of the loop counter “i” is less than or equal to 10. Each time the loop runs, it adds the current value of “i” to the variable “sum” and then increments “i” by 1. At the end of the loop, the program prints the final value of “sum”, which is the sum of the numbers from 1 to 10.

Using a “for” loop:

int sum = 0;
for (int i = 1; i <= 10; i++)
{
    sum += i;
}
printf("The sum of the numbers from 1 to 10 is %d\n", sum);

In this program, the “for” loop uses the “initialization” part to declare and initialize the loop counter “i” to 1. The “expression” checks whether “i” is less than or equal to 10, and the “increment” increments “i” by 1 after each iteration of the loop. The code block inside the “for” loop adds the current value of “i” to the variable “sum”. At the end of the loop, the program prints the final value of “sum”, which is the sum of the numbers from 1 to 10.

Using a “do-while” loop:

int sum = 0;
int i = 1;
do
{
    sum += i;
    i++;
}
while (i <= 10);
printf("The sum of the numbers from 1 to 10 is %d\n", sum);

In this program, the “do-while” loop runs at least once before checking the expression. Each time the loop runs, it adds the current value of “i” to the variable “sum” and then increments “i” by 1. The loop continues to run as long as “i” is less than or equal to 10. At the end of the loop, the program prints the final value of “sum”, which is the sum of the numbers from 1 to 10.

Previous
#4 Conditional in C Programming - if-else
Next
#06 Functions in C Programming - with Practical examples