ESc101 : Fundamental of Computing
I Semester 2008-09
Lecture 6
• While loop
• For loop
1
Motivation for LOOPS in a Program
Many computational problems which require performing similar or same tasks a
number of times.
Examples :
• Print a statement 100 times.
• Print all odd integers upto 1000.
• Print all prime integers upto 10000.
2
While Loop
Syntax :
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk
⇒}
3
While Loop
Execution of While loop :
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk
⇒}
⇒ statement b;
4
While Loop
Execution of While loop : first the condition is evaluated
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
5
While Loop
Execution of While loop : if condition is False
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
we exit the loop and IP jumps to statement b.
6
While Loop
Execution of While loop : if condition is true
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
the body of the loop is executed.
7
While Loop
Execution of While loop : if condition is true
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
the body of the loop is executed.
8
While Loop
Execution of While loop : if condition is true
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
the body of the loop is executed.
9
While Loop
Execution of While loop : the condition is checked again
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
10
While Loop
Execution of While loop : if condition is False
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
we exit the loop and IP jumps to statement b.
11
While Loop
Execution of While loop : if condition is True
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
we enter the loop again.
12
While Loop
Execution of While loop : if condition is True
⇒ statement a;
⇒ while(condition)
⇒{
⇒ statement1;
⇒ statement2;
⇒ ···
⇒ statementk;
⇒}
⇒ statement b;
we execute the body of the loop.
13
While Loop
statement_A;
while ( condition )
{
Body
}
statement_B;
14
While Loop
How to print a statement 10 times
class print10
{ public static void main(String args[])
{
while( ?? )
{
System.out.println(‘‘Welcome !’’);
}
}
Observations :
1. ?
2. ??
15
While Loop
How to print a statement 10 times
class print10
{ public static void main(String args[])
{
while( ?? )
{
System.out.println(‘‘Welcome !’’);
}
}
Observations :
1. The condition must change during an iteration
2. ??
16
While Loop
How to print a statement 10 times
class print10
{ public static void main(String args[])
{
while( ?? )
{
System.out.println(‘‘Welcome !’’);
}
}
Observations :
1. The condition must change during an iteration
2. The condition must be true for first 10 iteration and then become false.
17
While Loop
How to print a statement 10 times
class print10
{ public static void main(String args[])
{
while( ?? )
{
System.out.println(‘‘Welcome !’’);
}
}
Idea : keep a counter
1. The condition must change during an iteration
2. The condition must be true for first 10 iteration and then become false
18
While Loop
How to print a statement 10 times
class print10
{ public static void main(String args[])
{
while( ?? )
{
System.out.println(‘‘Welcome !’’);
counter = counter + 1;
}
}
Idea : keep a counter
1. The condition must change during an iteration
2. The condition must be true for first 10 iteration and then become false
19
While Loop
How to print a statement 10 times
class print10
{ public static void main(String args[])
{
int counter; counter = 1;
while(counter <=10)
{
System.out.println(‘‘Welcome !’’);
counter = counter + 1;
}
}
Idea : keep a counter
1. The condition must change during an iteration
2. The condition must be true for first 10 iteration and then become false
20
II : For Loop
21
For Loop
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
22
Execution of For Loop
as first step, stmt 1 is executed
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
23
Execution of For Loop
now the condition is evaluated.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
24
Execution of For Loop
if condition is false, IP goes to stmt B.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
25
Execution of For Loop
if condition is true, enter the Body.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
26
Execution of For Loop
execute the Body completely.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
27
Execution of For Loop
then execute stmt 2.(one cycle/iteration is completed)
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
28
Execution of For Loop
evaluate the condition again.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
29
Execution of For Loop
if condition is false, IP goes to stmt B.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
30
Execution of For Loop
if condition is true, enter the Body.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
31
Execution of For Loop
execute the Body completely.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
32
Execution of For Loop
then execute stmt 2.(second cycle/iteration is completed)
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
33
Execution of For Loop
now the condition is evaluated.
stmt_A;
for ( stmt_1 ; condition ; stmt_2 )
{
Body
}
stmt_B;
34
Equivalence between For loop and While loop
stmt_A; stmt_A;
for ( stmt_1 ; condition ; stmt_2 ) stmt_1;
{ while( condition )
{
Body
Body
}
stmt_2;
stmt_B;
}
stmt_B;
35
Homework
Write programs using for and while loop which can execute the following tasks :
1. For an arithmetic progression with first term a and common difference d, print
first 10 terms.
2. For a geometric progression with first term a and common ratio r, print first 15
terms.
a,d,r may be declared as variables of type int.
36