Core Java
Lesson 4—Using Loop Constructs in Java
© Simplilearn. All rights reserved.
Learning Objectives
At the end of this lesson, you should be able to:
Work with for loop
Work with while loop
Work with do...while loop
Compare loops
Use break and continue statements
Loops in Java
Enable you to execute blocks of statements multiple Start
times
Are controlled by Boolean expressions
Conditional Code
Types of loops:
If condition
is true
the for loop Condition
the while loop
If condition
the do…while loop is false
Exit
Loops in Java
Topic 1—for loop
for Loop
Commonly used for simple iterations for( init; condition; increment)
{
conditional code ;
}
Allows you to repeat a set of statements a Init
certain number of times until a condition is
matched
Has the following syntax: Condition
Increment is invoked If condition
Initialization is executed
after each iteration is true
once as the loop begins
through the loop
code block
If condition
is false
for (initialization; termination; increment) {
statement(s) increment
}
Termination evaluates
to false
for Loop (Contd.)
Examples:
public class Test public class Test
{ {
public static void main(String args[]) { public static void main(String args[]) {
for(int i = 1; i <= 10; i++) { for(int i = 10; i >= 1; i--) {
System.out.println("value of i : " + i ); System.out.println("value of i : " + i );
} }
} }
} }
Output Output
1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
Loops in Java
Topic 2—while loop
while Loop
while(condition)
Continually executes a block of statements as long {
conditional code ;
as a given statement is true }
Evaluates its expression at the top of the loop Condition
If condition
Has the following syntax: is true
code block
If condition
while (expression) {
statement(s) is false
}
while Loop (Contd.)
Examples:
public class Sample { public class Sample {
public static void main(String args[]) { public static void main(String args[]) {
int i=0; int i=10;
while (i<=10) { while (i>=1) {
System.out.println("value of i : " + i ); System.out.println("value of i : " + i );
i++; i--;
} }
} }
} }
Output Output
1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
Loops in Java
Topic 3—do...while loop
do…while Loop
Same as while loop, except that it is guaranteed to do{
execute at least one time conditional code ;
} while (condition)
code block
Evaluates its expression at the bottom of the loop,
instead of top If condition
is true
Has the following syntax:
Condition
do {
statement(s)
} while (expression);
If condition
is false
do…while Loop (Contd.)
Examples:
public class Sample { public class Sample {
public static void main(String args[]) { public static void main(String args[]) {
int i=0; int i=10;
do { do {
System.out.println("value of i : " + i ); System.out.println("value of i : " + i );
i++; i--;
} }
while (i<=10); while (i>=1);
} }
} }
Output Output
1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
Core Java
Topic 4—Compare Loops
Comparing Loops in Java
for while do while
• Any number of
• Zero or one iteration • At least one iteration
iterations
• When total iterations • When total iterations
• When total iterations
unknown known
known
Initialize variable(s) Execute code_block
false Test Boolean false Test Boolean Test Boolean true
expression expression expression
Update
true variable(s) true
false
Execute
Execute
code
code_block
block
Core Java
Topic 5—break and continue statements
break Statement
Allows you to prematurely exit from the loop statement
conditional
Has the following syntax: code
break; If condition
is true
break
Condition
If condition
is false
break Statement (Contd.)
Example:
public class Test {
public static void main(String args[]) {
int [] numbers = {5, 10, 15, 20, 25};
for(int x : numbers ) {
if( x == 20 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output
5 10 15
continue Statement
Allows you to skip over and jump to the end of the loop and
return control to the loop
Has the following syntax: conditional
code
continue;
If condition
is true
Condition continue
If condition
is false
continue Statement (Contd.)
Example:
public class Test {
public static void main(String args[]) {
int [] numbers = {5, 15, 25, 35, 45};
for(int x : numbers ) {
if( x == 25 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output
5 15 35 45
Key Takeaways
for loop allows you to repeat a set of statements a certain number of times until a
condition is matched.
while loop continually executes a block of statements as long as a given statement
is true.
do...while loop is the same as while loop, except that it is guaranteed to execute at
least one time.
break statement allows you to prematurely exit from the loop statement.
continue statement allows you to skip over and jump to the end of the loop and
return control to the loop.
Quiz
QUIZ
Which of these are exit controlled loops?
1
a. for
b. do…while
c. while
d. None
QUIZ
Which of these are exit controlled loops?
1
a. for
b. do…while
c. while
d. None
The correct answer is b. do…while loop
do…while() executes the body of loop first and then checks for condition. Thus, it is called exit-
controlled loop.
QUIZ
Which of the following are correct syntaxes that will not result in infinite loop?
2
a. for(int i=0;i<5;i++){}
b. for(int i=0,j=10;i<10;i++,j--){}
c. while(true){}
d. do {}while(true);
QUIZ
Which of the following are correct syntaxes that will not result in infinite loop?
2
a. for(int i=0;i<5;i++){}
b. for(int i=0,j=10;i<10;i++,j--){}
c. while(true){}
d. do {}while(true);
The correct answer is a and b.
Options “a” and “b” are the syntaxes of finite loop as the limit is already mentioned. In the other
syntaxes, no limit is mentioned. So “c” and “d” will fall into infinite loop.
Thank You