5.4. Looping Statement
Until now, we are able to execute instructions in sequential order, perform conditional execution of statements. C programming also provides us with functionality to execute a statement or set of statements repeatedly, such type of instructions are called a Looping Statement, which performs repeated execution of instructions a fixed number of times or until a particular condition is satisfied.we can represent the looping statement in flowchart as follows:
The above diagram clearly states that the instruction or set of instructions keeps on executing as long as the condition specified is TRUE, when the condition becomes FALSE, the looping statement is terminated and program controls transfer to the immediate next statement after the looping statement.
There are three ways by which we can repeat execution of instruction in C:
5.4.1 While loop
The while loop in C-programming can be used to execute, the given statements fixed number of times, the general form of while loop is as follows:
initialize loop counter variable
while(test loop counter variable using condition)
{
statement 1;
statement 2;
statement 3;
.
.
statement n;
increment or decrement loop counter variable
}
The above syntax states that:
Write a C program to print hello world 5 times.
#include <stdio.h>
int main() {
// Initialize loop counter variable
int counter = 0;
// While loop with the condition (counter < 5)
while (counter < 5) {
// Statement inside the loop
printf("Hello, world!\n");
// Incrementing the counter
counter++;
}
return 0;
}
In the above program:
Output:
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Write a C program, to print all the numbers from 1 to 10.
#include <stdio.h>
int main() {
// Initialize loop counter variable
int i = 1;
// While loop with the condition (i <= 10)
while (i <= 10) {
// Statement inside the loop to print the current value of i
printf("%d\n", i);
// Incrementing the counter
i++;
}
return 0;
}
Write a C program, to print all the numbers from 10 to 1.
#include <stdio.h>
int main() {
// Initialize loop counter variable
int i = 10;
// While loop with the condition (i >= 1)
while (i >= 1) {
// Statement inside the loop to print the current value of i
printf("%d\n", i);
// Decrementing the counter
i--;
}
return 0;
}
It is perfectly valid to use conditional statements or any valid C-statement within the body of the while loop. Let us understand the concept using following example:
Write a C program, to print all the even numbers from 1 to 10.
#include <stdio.h>
int main() {
// Initialize loop counter variable
int i = 1;
// While loop with the condition (i <= 10)
while (i <= 10) {
// Check if i is even
if (i % 2 == 0) {
// If i is even, print it
printf("%d\n", i);
}
// Incrementing the counter
i++;
}
return 0;
}
In the above program:
5.4.2 for loop
We can achieve a loop in a C program by either using for or while loop. For loop is more popular among developer compared to while loop as it allow us to write:
In a single line. Due to this ease of use, the for loop is more commonly used compared to while or do..while loop. The general form of for loop is as follows:
for(Initialize counter;test loop counter;update loop counter)
{
Statement 1;
Statement 2;
Statement 3;
.
Statement n
}
The above mentioned syntax of for loop, works as follows:
The diagram below also shows, the order of execution in for loop:
Write a C program to print Hello World 5 Times:
#include <stdio.h>
int main() {
int i;
// Iterate 5 times using a for loop
for (i = 0; i < 5; i++) {
// Print "Hello, world!"
printf("Hello, world!\n");
}
return 0;
}
Write a C program to print multiplication table of given number:
#include <stdio.h>
int main() {
int number;
// Input the number from the user
printf("Enter the number: ");
scanf("%d", &number);
// Print the multiplication table
printf("Multiplication table for %d:\n", number);
for (int i = 1; i <= 10; i++) {
printf("%d X %d = %d\n", number, i, number * i);
}
return 0;
}
5.4.3 do..while loop
The general form of do.. while loop is as follows:
initialize loop counter
do{
statement 1;
statement 2;
.
statement n;
update loop counter
}while(condition is true);
The major difference between while and do while loop is:
Let us write a C program , to print all the numbers from 0 to 5:
#include <stdio.h>
int main() {
int i = 0;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
The same program can be written using do while loop as follows:
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
return 0;
}
Write a C program ,that asks a user to enter a number as long as user wants, and find sum of all these numbers:
#include <stdio.h>
int main() {
int number, sum = 0; // Initialize sum to 0
char choice = 'y';
do {
printf("Enter a number: ");
scanf("%d", &number);
sum += number;
printf("Do you want to enter another number y or n: ");
scanf(" %c", &choice);
} while (choice=='y'); // Continue looping until choice=='n
printf("The sum of all entered numbers is: %d\n", sum);
return 0;
}
Output:
Enter a number: 2
Do you want to enter another number y or n: y
Enter a number: 3
Do you want to enter another number y or n: y
Enter a number: 4
Do you want to enter another number y or n: y
Enter a number: 4
Do you want to enter another number y or n: n
The sum of all entered numbers is: 13