chapter-5-140331202556-phpapp02 (1)
chapter-5-140331202556-phpapp02 (1)
Repetition
Outlines
Repetition Statements
while statement
do..while statement
for statement
Nested loops
Repetition Control Structures
Repetition Statements
Repetition statement (or loop) a block of code to be
executed for a fixed number of times or until a certain
condition is met.
In JAVA, repetition can be done by using the
following repetition statements:
a) while
b) do…while
c) for
The while statement
The while statement evaluates
expression/condition, which must return a boolean
value. If the expression/condition is true, the
statement(s) in the while block is/are executed.
Syntax:
while (expression/condition)
Statement(s);
It continues testing the expression/condition and
executing its block until the expression/condition
evaluates to false
The while statement
Example 1
int i=1;
while (i<5){
System.out.print(i + “”);
i++;
}
Output ? 1234
Example 2
Output ? SUM : 1
SUM : 7
Good Bye
The do…while statement
It is similar to while loops except it first executes the
statement(s) inside the loop, and then evaluates the
expression/condition. If the expression is true, the
statement(s) in the do loop is/are executed again.
Syntax
do
statement(s);
while (expression);
It continues executing the statement until the
expression/condition becomes false.
Note that the statement within the do loop is always
executed at least once.
The do…while statement
Example 3
int i=0;
do{
System.out.print(i +
“”);
i++;
}while(i<=3);
Output ? 0123
Example 4
Output ? SUM : 2
SUM : 9
The for statement
Usually used when a loop will be executed a set
number of times.
Syntax:
for(initial statement; loop condition; update statement)
statement(s);
The for loop executes as follow:
1) The initial statement is executed.
2) The loop expression/condition is evaluated. If it is TRUE,
the loop statement is executed followed by the execution
of the update statement
3) Repeat step 2) until the loop condition evaluates to
FALSE.
The for statement
Example 5
Output ? 1234
Example 6
00
Output ? 01
10
11
20
21
Infinite Loop
By using any repetition statements, make sure that
the loop will eventually terminate.
An infinite loop occurs when a condition always
evaluates to true and continues to execute endlessly.
int product =0;
for (product = 0;product < 10;)
{ product = product * 2;
System.out.println(product);
}
Repetition Control Structures
Exercise
Counter Controlled Loop
import java.util.Scanner;
class sentinelLoop {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an Integer, or -1 to stop: ");
int choice= input.nextInt();
while (choice!=-1)
{
System.out.println("INSIDE LOOPING");
System.out.print("Enter an Integer, or -1 to stop: ");
choice= input.nextInt();
}
System.out.println("Sentinel value detected. Good Bye.");
}
}
Example 11 ….
OUTPUT?
Enter an Integer, or -1 to stop: 2
INSIDE LOOPING
Enter an Integer, or -1 to stop: 5
INSIDE LOOPING
Enter an Integer, or -1 to stop: 0
INSIDE LOOPING
Enter an Integer, or -1 to stop: -1
Sentinel value detected. Good Bye.
back
Flag Controlled Loop
Use a boolean variable to control the loop
back
Exercise 1:
What is the output of the following program?
public class LoopExercise1
{
public static void main (String args[])
{
int choice=1, total=0;
while (choice <4){
total = choice++;
System.out.print(total); }
}
}
Exercise 2:
What is the output of the following program?
public class LoopExercise2
{
public static void main (String args[]){
for (int number =2; number <20; number++)
{
number = number *2;
if (number <15)
System.out.println(number);}
}
}
Exercise 3:
How many times is the following loop body repeated?