Lesson 10. Looping Statement
Lesson 10. Looping Statement
1. For ..Loop
2. While Loop
3. Do while loop
4. Break
5. continue
For Loop
SYNTAX:
statement/s;
While Loop
A while loop statement repeatedly executes a target statement as long as a
given condition is true.
However, when the condition becomes false, the program control passes to the
line immediately following the loop.
SYNTAX:
Do-While loop
The do-while loop is similar to the while loop except that the condition is always
executed after the body of a loop. It is also called an exit-controlled loop.
SYNTAX:
do {
statements
while (expression);
As we learned in using a while loop, the body is executed if and only if the
condition is true. In some cases, we have to execute a body of the loop at least
once even if the condition is false. This type of operation can be achieved by
using a do-while loop.
In the do-while loop, the body of a loop is always executed at least once. After
the body is executed, then it checks the condition. If the condition is true, then it
will again execute the body of a loop otherwise control is transferred out of the
loop.
Similar to the while loop, once the control goes out of the loop the statements
which are immediately after the loop is executed.
The other difference between the while and do-while loop is that in while loop
the while is written at the beginning. In do-while loop, the while condition is
written at the end and terminates with a semi-colon (;)
Break Statement
The break statement is used mainly in in the switch statement. It is also useful
for immediately stopping a loop.
}}
Output:
5
Continue Statement
When you want to skip to the next iteration but remain in the loop, you should
use the continue statement.
For example:
#include <stdio.h> int main() { int nb = 7; while (nb > 0) { nb--; if (nb ==
5) continue;
printf("%d\n", nb);
}}
Output:
FOR STATEMENTS
int main()
int ntimes;
}
printf("\nProgrammed by: Benjamin Ramirez");
return 0;
int main()
int ntimes;
printf("*\n");
return 0;
int main()
int ntimes;
char name[50];
scanf("%s", &name);
}
printf("\nProgrammed by: Benjamin Ramirez");
return 0;
int main()
char name[50];
scanf("%d", &howmany);
scanf("%s", &name);
return 0;
}
int main()
int order;
float price = 0;
scanf("%d", &hm);
switch(order)
{
case 1:
scanf("%d", &quantity);
break;
case 2:
scanf("%d", &quantity);
break;
case 3:
scanf("%d", &quantity);
break;
case 4:
scanf("%d", &quantity);
printf("You ordered %d Buffalo Cheese Steak(s) worth %.2f PHP.\n", quantity, price);
break;
case 5:
printf("Enter number of pcs: ");
scanf("%d", &quantity);
break;
default:
return 0;
return 0;
WHILE STATEMENTS:
int main()
int n;
n=1;
while(n<=5)
printf("\n Benjamin\n");
n++;
}
return 0;
int main()
int n;
n=1;
while(n<=5)
n++;
return 0;
int main()
int n, ntimes;
scanf("%d",&ntimes);
while(n<=ntimes)
{
n++;
return 0;
int main()
int n, ntimes;
scanf("%d",&ntimes);
n=1;
while(n<=ntimes)
n++;
return 0;
}
DO-WHILE STATEMENTS:
int main()
int n, ntimes;
scanf("%d",&ntimes);
n=1;
do{
n++;
}while(n<=ntimes);
return 0;