0% found this document useful (0 votes)
51 views

Repetition Statements (Loops)

This document discusses repetition statements or loops in Java. There are three types of loops - while, do, and for loops. Loops allow code to execute statements repeatedly as long as a condition is true. The while loop checks if a condition is true before executing the loop body. It will continue to execute the body repeatedly as long as the condition remains true. Loops can be nested, with one loop inside the body of another loop. Infinite loops occur when the condition is never made false, causing the loop to run indefinitely.

Uploaded by

Neha Bansude
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Repetition Statements (Loops)

This document discusses repetition statements or loops in Java. There are three types of loops - while, do, and for loops. Loops allow code to execute statements repeatedly as long as a condition is true. The while loop checks if a condition is true before executing the loop body. It will continue to execute the body repeatedly as long as the condition remains true. Loops can be nested, with one loop inside the body of another loop. Infinite loops occur when the condition is never made false, causing the loop to run indefinitely.

Uploaded by

Neha Bansude
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter 4

Repetition Statements (loops)

1
Loops – While, Do, For
• Repetition Statements
– While
– Do
– For

2
Repetition Statements
• Repetition statements allow us to execute a
statement or a block of statements multiple times
• Often they are referred to as loops
• Like conditional statements, they are controlled by
boolean expressions
• Java has three kinds of repetition statements:
while
do
for
• The programmer should choose the right kind of
loop statement for the situation
3
The while Statement
• A while statement has the following syntax:
while ( condition )
statement;
• If the condition is true, the statement is
executed
• Then the condition is evaluated again, and if it is
still true, the statement is executed again
• The statement is executed repeatedly until the
condition becomes false

4
Logic of a while Loop

condition
evaluated

true false

statement

5
The while Statement
• An example of a while statement:
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

• If the condition of a while loop is false


initially, the statement is never executed
• Therefore, the body of a while loop will
execute zero or more times
6
animation

Trace while Loop


Initialize count
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

7
animation

Trace while Loop, cont.


(count < 2) is true
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

8
animation

Trace while Loop, cont.


Print Welcome to Java
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

9
animation

Trace while Loop, cont.


Increase count by 1
int count = 0; count is 1 now

while (count < 2)


{
System.out.println("Welcome to Java!");
count++;
}

10
animation

Trace while Loop, cont.


(count < 2) is still true since count
int count = 0; is 1

while (count < 2)


{
System.out.println("Welcome to Java!");
count++;
}

11
animation

Trace while Loop, cont.


Print Welcome to Java
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}

12
animation

Trace while Loop, cont.


Increase count by 1
int count = 0; count is 2 now

while (count < 2)


{
System.out.println("Welcome to Java!");
count++;
}

13
animation

Trace while Loop, cont.


(count < 2) is false since count is 2
int count = 0; now

while (count < 2)


{
System.out.println("Welcome to Java!");
count++;
}

14
animation

Trace while Loop


The loop exits. Execute the next
int count = 0; statement after the loop.

while (count < 2)


{
System.out.println("Welcome to Java!");
count++;
}

15
Example (Average.java)

16
Infinite Loops
• Executing the statements in the body of a while
loop must eventually make the condition false
• If not, it is called an infinite loop, which will
execute until the user interrupts the program
• This is a common logical error
• You should always double check the logic of a
program to ensure that your loops will terminate

17
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (count <= 25)
{
System.out.println(count);
count = count - 1;
}

• This loop will continue executing until the


user externally interrupts the program.

18
Nested Loops
• Similar to nested if statements, loops can
be nested as well
• That is, the body of a loop can contain
another loop
• For each iteration of the outer loop, the
inner loop iterates completely

19
Nested Loops
• How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
System.out.println ("Here");
count2++;
}
count1++;
}
10 * 20 = 200
20

You might also like