IMPLEMENTING WHILE AND DO
WHILE LOOP
Chapter 5.1:
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
Output ? 1 2 3 4
int i=1;
while (i<5){
System.out.print(i + “”);
i++;
}
Example 2
Output ? SUM : 1
SUM : 7
Good Bye
int sum=0, number =1;
while (number <= 10)
{
sum+=number;
number = number + 5;
System.out.println(“SUM :” + sum);
}
System.out.println(“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
Output ? 0 1 2 3
int i=0;
do{
System.out.print(i + “”);
i++;
}while(i<=3);
Example 4
Output ? SUM : 2
SUM : 9
int sum=0, number =2;
do{
sum+=number;
number = number + 5;
System.out.println(“SUM :” + sum);
} while (number <= 10);

Chapter 5.1

  • 1.
    IMPLEMENTING WHILE ANDDO WHILE LOOP Chapter 5.1:
  • 2.
    Repetition Statements  Repetitionstatement (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
  • 3.
    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
  • 4.
  • 5.
    Example 1 Output ?1 2 3 4 int i=1; while (i<5){ System.out.print(i + “”); i++; }
  • 6.
    Example 2 Output ?SUM : 1 SUM : 7 Good Bye int sum=0, number =1; while (number <= 10) { sum+=number; number = number + 5; System.out.println(“SUM :” + sum); } System.out.println(“Good Bye”);
  • 7.
    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.
  • 8.
  • 9.
    Example 3 Output ?0 1 2 3 int i=0; do{ System.out.print(i + “”); i++; }while(i<=3);
  • 10.
    Example 4 Output ?SUM : 2 SUM : 9 int sum=0, number =2; do{ sum+=number; number = number + 5; System.out.println(“SUM :” + sum); } while (number <= 10);