Topic : While , For , Do-While Loop
Guided By :
Branch : Batch :
Name ENROLLMENT NO.
Abhishek Chokshi 140120109005
Raj Shah
Kaveesh Raval
140120109054
140120109016
 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:
 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.
Logical expression that determines
whether the action isto be executed
while ( Expression) Action
Action to be iteratively
performed until logical
expression isfalse
 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
statement
true false
condition
evaluated
 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
 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.
 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
true
condition
evaluated
statement
false
do {
statement-1;
statement-2;
……
statement-k;
} while(condition);
Initialize
Statement-1
Statement-2
Statement-k
Is condition
TRUE?
N
Y
/* 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.
 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.
 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
statement
true
condition
evaluated
false
increment
initialization
 A for loop is functionally equivalent to the
following while loop structure:
initialization;
while ( condition ){
statement;
increment;
}
 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
 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);
}
 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
 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
While , For , Do-While Loop

While , For , Do-While Loop

  • 1.
    Topic : While, For , Do-While Loop Guided By : Branch : Batch :
  • 2.
    Name ENROLLMENT NO. AbhishekChokshi 140120109005 Raj Shah Kaveesh Raval 140120109054 140120109016
  • 3.
     Repetition statementsallow 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 syntaxof 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 thatdetermines whether the action isto be executed while ( Expression) Action Action to be iteratively performed until logical expression isfalse
  • 6.
     A whilestatement 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
  • 7.
  • 8.
     An exampleof 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
  • 10.
     The syntaxof 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
  • 12.
  • 13.
  • 14.
  • 15.
    /* Find aneven 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 syntaxof 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 forstatement 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
  • 18.
  • 19.
     A forloop is functionally equivalent to the following while loop structure: initialization; while ( condition ){ statement; increment; }
  • 20.
     An exampleof 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 incrementsection 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 expressionin 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 thebreak 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