Sunteți pe pagina 1din 18

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 return
a boolean value. If true, the statement(s) in the while block
is/are executed.

Syntax:
while (expression/condition)
Statement(s);
Examples While…
int i=1;
while (i<5){ Output ?
System.out.print(i + “”); 1234
i++;
}

int sum=0, number =1; Output ?


while (number <= 10)
{ SUM : 1
SUM : 7
sum+=number;
Good Bye
number = number + 5;
System.out.println(“SUM :” + sum);
}
System.out.println(“Good Bye”);
The do…while statement
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);

Note that the statement within


the do loop is always executed
at least once.
Examples do…while
int i=0;
do{
Output ?
System.out.print(i + “”);
i++; 0123
}while(i<=3);

Output ?
int sum=0, number =2;
do{ SUM : 2
sum+=number; SUM : 9
number = number + 5;
System.out.println(“SUM :” + sum);
} while (number <= 10);
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.
It is possible to initialize multiple variable in the initialization
block of the for loop by separating it by comma as given in
the below example

for(int i=0, j =5 ; i < 5 ; i++)

It is also possible to have more than one increment or


decrement section as well as given below

for(int i=0; i < 5 ; i++, j--)


Examples for loop
Output ?
for (i=1; i<5; i++)
System.out.print(i); 1234

for (i=1; i<3; i++){ Output ?


System.out.print(“YAHOO” + “”);
System.out.print(“***”); YAHOO ***YAHOO ***
}

for (i=1; i<=5; i++)


System.out.print(“YAHOO”);
Output ?
System.out.print(“***”);

YAHOO YAHOO YAHOO YAHOO YAHOO ***


Nested Loops
loop inside the body of another loop. All types of loops may be
nested, the most commonly nested loops are for loops.

Nested for Loops

When working with nested


loops, the outer loop
changes only after the
inner loop is completely
finished (or is interrupted.).
Examples Nested loop
Output
int num1, num2;
for(num2 = 0; num2 <= 2; num2++) 00
{ 01
for(num1 = 0; num1 <= 1; num1++) 10
{ 11
System.out.println(num2 + " " + num1); 20
}
} 21

int product =0;


Beware of
for (product = 0;product < 10;)
infinite loop!! { product = product * 2;
System.out.println(product);
}
Controlling the Repetition
Control using counter Flag Controlled Loop

int num =1; boolean found = false;


while (num < 10){ while (!found){
System.out.print (num); :
num = num +2; :
if(expression)
} found = true;
}
Output ? 13579
When unsure how many times to loop, use a
Sentinel Controlled Loop "special" or sentinel value to indicate that
the loop is to end. This must be a value that
doesn't occur normally in the data.

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.");
}
}

Enter an Integer, or -1 to stop: 2


INSIDE LOOPING
Enter an Integer, or -1 to stop: 5
INSIDE LOOPING
OUTPUT: Enter an Integer, or -1 to stop: 0
INSIDE LOOPING
Enter an Integer, or -1 to stop: -1
Sentinel value detected. Good Bye.
Exercises
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); }
}
}
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);}
}
}
How many times is the following loop body repeated?

public class LoopExercise3 {


public static void main (String args[])
{
int i=1;
do {
if ((i % 2)== 0)
System.out.print(i);
i++;
} while(i<5);
}
}
Write a java code that will generate the following pyramid
structure:
*
**
***
****
*****

public class JavaPyramid1 {

public static void main(String[] args) {

for(int i=1; i<= 5 ;i++){

for(int j=0; j < i; j++){


System.out.print("*");
}

//generate a new line


System.out.println("");
}
}
}
***** *
**** **
*** ***
** ****
* *****
*****
****
***
**
*

S-ar putea să vă placă și