Professional Documents
Culture Documents
1 5 2 P 603c5d883c701 File
1 5 2 P 603c5d883c701 File
1 5 2 P 603c5d883c701 File
Written by
Yogita Jore
Head, Department of Information Technology (NBA Accredited),
Vidyalankar Polytechnic, Mumbai
Unit Outcome 5:
Develop Programs using relevant
control structure to solve the given
problem.
Learning Outcome 5b:
Students should understand the use of
looping statements.
What we will learn today-Looping Statements
Key takeaways
Where to use looping statements in given problem?
Yogita Jore
Head, Department of Information Technology (NBA Accredited), Vidyalankar Polytechnic,
Mumbai
► while
► do while
► for
► For each
The Java while loop is used to iterate a part of the program several times. Syntax:
If the number of iteration is not fixed, it is recommended to use while loop. while(condition)
{
Example: //code to be executed
public class WhileExample }
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
}
}
Page 8 Maharashtra State Board of Technical Education
do-while loop
The Java do-while loop is used to iterate a part of the program several times.
If the number of iteration is not fixed and you must have to execute the loop at least once, it is
recommended to use do-while loop.
The Java do-while loop is executed at least once because condition is checked after loop body.
Example: Syntax:
public class DoWhileExample
{ do
public static void main(String[] args) {
{ //code to be executed
int i=1;
do }
{ while(condition);
System.out.println(i);
i++;
}
while(i<=10);
}
}
}
}
Page 9 Maharashtra State Board of Technical Education
for loop
The Java for loop is used to iterate a part of the program several times.
If the number of iteration is fixed, it is recommended to use for loop.
Example:
public class ForExample Syntax:
{ for(initialization;condition;incr/decr)
public static void main(String[] args) {
{ //statement or code to be executed
for(int i=1;i<=10;i++) }
{
System.out.println(i);
}
}
}