Java Looping Statements
Java Looping Statements
Syntax
while (boolean_expression) {
statement1;
statement 2;
…..}
The statements in while loop are executed as long as the boolean expression is true
Illustration of a while statement
do-while Statement
It is similar to while loop except that the do-while execute the block once, and
then checks the while condition.
Real Time Example : ATM Machine
Illustration of a do while statement
For Statement
For statement is similar to while loop is used to repeat the execution
of the code till a condition is met.
Syntax:
for(initialization; loopCondition; iteration) {
statements;
}
The initialization allows to declare and/or initialize loop variables, and is executed
only once.
The loopCondition compares the loop variable to some limit value. If the loop
condition is not met it is broken.
The iteration usually increments or decrements the values of the loop variables
Illustration of a for statement
Initialize Loop
counter
Update
Counter
Is
Yes Execute block
conditio
n true?
of commands
No
Program continues..
For Statement Example
class Example{
public static void main(String []args){
for(int i=1; i< 10; i++){
Statement
System.out.println(“The Number is “+i); Executed in
loop
}
Loop Value Condition for loop, Loop value
}
Initialized to Loop executed till incremented.
1. value is < 10
Summarize
Time To Reflect
Associates to reflect the following topics before proceeding.