0% found this document useful (0 votes)
2 views5 pages

Looping in C

The document explains looping in C programming, detailing three types of loops: for loop, while loop, and do...while loop. It provides syntax and examples for each loop type, highlighting their execution flow and differences in condition testing. The document also distinguishes between entry-controlled and exit-controlled loops.

Uploaded by

manojgamer.15464
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Looping in C

The document explains looping in C programming, detailing three types of loops: for loop, while loop, and do...while loop. It provides syntax and examples for each loop type, highlighting their execution flow and differences in condition testing. The document also distinguishes between entry-controlled and exit-controlled loops.

Uploaded by

manojgamer.15464
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

looping in C

In programming, a loop is used to repeat a block of


code until the specified condition is met.

C programming has three types of loops:

1. for loop

2. while loop

3. do...while loop

How for loop work?


The initialization statement is executed only once.

Then, the test expression is evaluated. If the test expression is evaluated as


false, the for loop is terminated.

looping in C 1
However, if the test expression is evaluated to be true, statements inside the
body of the for loop are executed, and the update expression is updated.

Again the test expression is evaluated.

for Loop
The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)


{
// statements inside the body of loop
}

Example 1: for loop


// Print numbers from 1 to 10
#include <stdio.h>

int main() {
int i;

for (i = 1; i < 11; i++)


{
printf("%d ", i);
}
return 0;
}

1 2 3 4 5 6 7 8 9 10

while loop
The syntax of the while loop is:

looping in C 2
while (testExpression) {
// the body of the loop
}

Example 1: while loop


// Print numbers from 1 to 5

#include <stdio.h>
int main() {
int i = 1;

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

return 0;
}

Output

1
2
3
4
5

looping in C 3
do...while loop
The do..while loop is similar to the while loop with one important difference. The
body of do...while loop is executed at least once. Only then, the test expression is
evaluated

The syntax of the do...while loop is:

do {
// the body of the loop
}
while (testExpression);

Example 2: do...while loop


// Program to add numbers until the user enters zero

#include <stdio.h>
int main() {
double number, sum = 0;

// the body of the loop is executed at least once


do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);

printf("Sum = %.2lf",sum);

return 0;
}

looping in C 4
Entry Control Exit Control

The condition is tested at the beginning of The condition is tested at the end of the Loop
the Loop before the loop body executes. after the loop body is executed at least once.

The loop body may not be run if the The loop body runs at least once, even if the
condition is false. condition is false.

While Loop, For are some popular Entry Do-while is usually used as Exit Controlled
Controlled Loops. Loop.

Since the condition is tested at the Exit-controlled loops consistently execute the
beginning of the Loop, the loop body's loop body at least once, which can be less
code can be skipped if it is false. efficient if the condition is false.

It is mostly used when we know how many


It is used when the loop body’s code needs to
iterations are required and the loop
be executed at least once.
condition is always met.

looping in C 5

You might also like