while and do while
while and do while
1. for loop
2. while loop
3. do...while loop
5. while (testExpression) {
6. // the body of the loop
7. }
#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 <=
do {
// the body of the loop
}
while (testExpression);
#include <stdio.h>
int main() {
double number, sum = 0;
printf("Sum = %.2lf",sum);
return 0;
}
Run Code
Output