Chapter 5
Chapter 5
Chapter 5
Iteration
• Here are many situation when you want to
execute a block of statements several number
of times in your applications.
• Loops can execute a block of code a number
of times. Types of loop:
– For Loop.
– While loop.
– Do.. While loop.
For Loop
• The for loop in Java is useful for iterating over
arrays and for sequential processing.
• Syntax:
• for (initialization; termination; increment)
• {
• statement(s)
• }
For Loop
• initialization: is executed before the loop (the
code block) starts.
• termination: defines the condition for running
the loop (the code block).
• increment: is executed each time after the
loop (the code block) has been executed.
Iteration
For loop
• The for loop initialize the value before the first step.
• Then checking the condition against the current
value of variable and execute the loop statement
and then perform the step taken for each execution
of loop body.
• For-loops are also typically used when the number
of iterations is known before entering the loop.
• For-loops are the shorthand way to make loops
when the number of iterations is known, as every
for-loop could be written as a while-loop.
Example
•
• int counter = 4;
• for (int i= 1; i< = counter; i++) {
• System.out.println("Current value of I is: " +
i);
• }
Infinite loop
• All of the expressions of the for loop
statements are optional.
• A loop becomes infinite loop if a condition
never becomes false.
• You can make an endless loop by leaving the
conditional expression empty.
Infinite loop
• All of the expressions of the for loop statements
are optional.
• A loop becomes infinite loop if a condition
never becomes false.
• You can make an endless loop by leaving the
conditional expression empty.
• for ( ; ; ) {
• System.out.println("Infinite Loop!!!");
• }
While loop
• A while loop is a control flow statement that
allows code to be executed repeatedly based
on a given Boolean condition.
• Syntax:
• while (condition){
• statements;
• }
While loop
• The expression (condition) in the while loop is
evaluated, and if the expression (condition)
is true , the code within the block is executed.
• This repeats until the expression(condition)
becomes false .
• The expression(condition) should be updated
during the repetitions, otherwise the program
will never "break out" of while and lead to an
infinite loop.
Example of While loop
•
• class TestClass
• {
• public static void main (String[] args)
• {
• int cnt = 1;
• while (cnt < = 5)
• {
• System.out.println("The value of cnt is : " + cnt);
• cnt = cnt + 1;
• }
• }
• }
do...while loop
• A do-while loop is similar to while loop statement but the do-
while loop, the loop body will be executed first, then condition
is evaluated.
• If the condition is true, the loop body will be executed.
Otherwise the loop will be terminated.
• The advantage of a do...while loop is that it executes the block
of code at least once , and then repeatedly executes the block
depending on the condition.
• Syntax:
– do {
– statements;
– } while (condition);
Example
•
• class TestClass
• {
• public static void main (String[] args)
• {
• boolean enter=false;
• do
• {
• System.out.println("Enter in do..while loop");
• }while (enter);
• }
• }
END