3. Repetition statements allow us to execute a
statement multiple times
Often they are referred to as loops
Like conditional statements, they are
controlled by boolean expressions
There are three kinds of repetition
statements:
4. The syntax of while statement in C:
while (loop repetition condition)
statement
Loop repetition condition is the condition
which controls the loop.
The statement is repeated as long as the loop
repetition condition is true.
A loop is called an infinite loop if the loop
repetition condition is always true.
5. Logical expression that determines
whether the action isto be executed
while ( Expression) Action
Action to be iteratively
performed until logical
expression isfalse
6. A while statement has the following syntax:
while ( condition ){
statement;
}
• If the condition is true, the statement is executed
• Then the condition is evaluated again, and if it is still true, the
statement is executed again
• The statement is executed repeatedly until the condition becomes
false
8. An example of a while statement:
int count = 1;
while (count <= 5){
System.out.println (count);
count++;
}
• If the condition of a while loop is false initially, the statement is
never executed
• Therefore, the body of a while loop will execute zero or more times
9.
10. The syntax of the do-while statement in
C:
do
statement
while (loop repetition condition);
The statement is first executed.
If the loop repetition condition is
satisfied, the statement is repeated else,
the repetition of the loop is stopped.
11. Syntax
do Action
while
(Expression)
Semantics
◦ Execute Action
◦ If Expression is true
then execute Action
again
◦ Repeat this process
until Expression
evaluates to false
Action is either a
single statement or a
group of statements
within curly braces
Action
true
false
Expression
15. /* Find an even number input */
do{
printf(“Enter a value:n”);
scanf(“%d”,&num);
}while(num%2!=0)
printf(“n%d”,num);
This loop will repeat if the user inputs
odd number.
16. The syntax of for statement in C:
for (initialization expression;
loop repetition condition;
update expression)
statement
The initialization expression set the initial
value of the loop control variable.
The loop repetition condition test the value of
the loop control variable.
The update expression update the loop
control variable.
17. A for statement has the following syntax:
for ( initialization ; condition ; increment ){
statement;
}
The initialization
is executed once
before the loop begins
The statement is
executed until the
condition becomes false
The increment portion is executed at
the end of each iteration
19. A for loop is functionally equivalent to the
following while loop structure:
initialization;
while ( condition ){
statement;
increment;
}
20. An example of a for loop:
for (int count=1; count <= 5; count++){
System.out.println (count);
}
• The initialization section can be used to declare a variable
• Like a while loop, the condition of a for loop is tested prior to
executing the loop body
• Therefore, the body of a for loop will execute zero or more times
21. The increment section can perform any
calculation
• A for loop is well suited for executing statements a specific
number of times that can be calculated or determined in advance
for (int num=100; num > 0; num -= 5){
System.out.println (num);
}
22. Each expression in the header of a for loop is
optional
If the initialization is left out, no initialization is
performed
If the condition is left out, it is always considered
to be true, and therefore creates an infinite loop
◦ We usually call this a “forever loop”
If the increment is left out, no increment
operation is performed
23. Remember the break keyword that we used
to stop a switch statement from executing
more than one statement?
break can also be used to exit an infinite
loop
But it is almost always best to use a well-
written while loop