A loop is a fundamental control structure that allows you to execute a block of instructions repeatedly. It enables you to automate repetitive tasks by performing a particular action multiple times until a specific condition is met. The action or set of instructions within the loop is known as the loop body.
The loop operates through iterations, which represent each individual execution of the loop body. With each iteration, the loop checks a condition, and if the condition is true, it executes the loop body again. This process continues until the condition becomes false, at which point the loop terminates, and the program proceeds to the next line of code after the loop.
C offers three main types of loops.
The for
loop
The while
loop
The do-while
loop
for
loopThe for
loop is the most commonly used loop in C. It allows us to repeat a block of code a fixed number of times. This type of loop is particularly useful when we know the exact number of iterations required. The general structure of a for loop is as follows:
for (initialization; condition; update) {// Code to be executed in each iteration}
A for
loop consists of three main components:
Initialization: Setting the initial value of a control variable.
Condition: Specifying a condition that must be true for the loop to continue.
Update: Modifying the control variable after each iteration.
Note: All three components (initialization, condition, and update) are optional. You can omit any one or all three components.
Here’s a simple example of a for
loop that prints numbers from 1 to 5:
#include<stdio.h>int main(){for (int i = 1; i <= 5; i++) //i=1: start with one, i<=5: repeat until i is 5, i++: increment i each time{printf("%d ", i); //loop body to repeat}return 0;}
while
loopThe while
loop is useful when you want to repeat a block of code as long as a specific condition remains true. Unlike the for
loop, the while
loop doesn’t require an explicit initialization or update.
It relies on a condition to determine whether the loop should continue or terminate. The basic structure of a while loop is as follows:
while (condition) {// Code to be executed in each iteration}
The while loop will continue executing the code inside its block until the condition
becomes false. For example, let's print the Fibonacci sequence up to a given limit:
#include <stdio.h>int main() {int a = 0, b = 1, c; //a,b,c for fibonacci calculationsint limit=10; //limit at which fibonacci series should terminatewhile (a <= limit) { //if number 'a' is less than limit, execute the loop bodyprintf("%d ", a); //print 'a'c = a + b; //fibonacci logica = b;b = c;}return 0;}
do-while
loopThe do-while
loop is similar to the while
loop, but with one key difference. In a do-while
loop, the code block is executed first, and then the condition is checked. The structure of a do-while loop is as follows:
do {// Code to be executed in each iteration} while (condition);
The do-while
guarantees that the loop executes at least once, regardless of the condition’s initial state. For instance, let's take input from the user until they enter a positive number:
#include <stdio.h>int main() {int num; //variable to take inputdo {printf("Enter a positive number \n"); //take input atleast oncescanf("%d", &num);} while (num <= 0); //take input until num gets positiveprintf("You entered a positive number: %d\n", num);return 0;}//Sample input: '-2 -3 -4 1'
Enter the input below
C offers three main types of loops— for
, while
and do-while
. The for
loop is ideal when the exact number of iterations is known beforehand, providing a concise and controlled approach. On the other hand, when the number of iterations depends on a specific condition, the while
or do-while
loop becomes more appropriate. For situations where the loop body must run at least once, regardless of the condition's truth value, the do-while
loop should be preferred.
Free Resources