0% found this document useful (0 votes)
5 views

while and do while

The document explains three types of loops in C programming: for loop, while loop, and do...while loop. It details the syntax and functioning of the while loop and do...while loop, including example code for each. The while loop continues executing as long as a test expression is true, while the do...while loop guarantees at least one execution of its body before evaluating the test expression.

Uploaded by

Rama Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

while and do while

The document explains three types of loops in C programming: for loop, while loop, and do...while loop. It details the syntax and functioning of the while loop and do...while loop, including example code for each. The while loop continues executing as long as a test expression is true, while the do...while loop guarantees at least one execution of its body before evaluating the test expression.

Uploaded by

Rama Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

C programming has three types of loops.

1. for loop

2. while loop

3. do...while loop

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

5. while (testExpression) {
6. // the body of the loop
7. }

How while loop works?


 The while loop evaluates the testExpression inside the parentheses () .

 If testExpression is true, statements inside the body of while loop are


executed. Then, testExpression is evaluated again.
 The process goes on until testExpression is evaluated to false.
 If testExpression is false, the loop terminates (ends).

// Print numbers from 1 to 5

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

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

return 0;
}
Here, we have initialized i to 1.
1. When i = 1, the test expression i <= 5 is true. Hence, the body of
the while loop is executed. This prints 1 on the screen and the value of i is
increased to 2.
2. Now, i = 2, the test expression i <= 5 is again true. The body of
the while loop is executed again. This prints 2 on the screen and the value
of i is increased to 3.
3. This process goes on until i becomes 6. Then, the test expression i <=

5 will be false and the loop terminates.


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);

How do...while loop works?


 The body of do...while loop is executed once. Only then,
the testExpression is evaluated.
 If testExpression is true, the body of the loop is executed
again and testExpression is evaluated once more.
 This process goes on until testExpression becomes false.
 If testExpression is false, the loop ends.

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;
}
Run Code

Output

Enter a number: 1.5


Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70

You might also like