SlideShare a Scribd company logo
Chapter 4
Repetition Statements (loops)
1
2
Loops – While, Do, For
• Repetition Statements
– While
– Do
– For
3
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
4
The while Statement
• A while statement has the following syntax:
• 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
while ( condition )
statement;
Logic of a while Loop
statement
true false
condition
evaluated
5
6
The while Statement
• An example of a while statement:
• 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
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Trace while Loop
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Initialize count
animation
7
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
(count < 2) is true
animation
8
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Print Welcome to Java
animation
9
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Increase count by 1
count is 1 now
animation
10
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
(count < 2) is still true since
count is 1
animation
11
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Print Welcome to Java
animation
12
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
Increase count by 1
count is 2 now
animation
13
Trace while Loop, cont.
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
(count < 2) is false since count is
2 now
animation
14
Trace while Loop
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
The loop exits. Execute the next
statement after the loop.
animation
15
16
Example (Average.java)
17
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
18
Infinite Loops
• An example of an infinite loop:
• This loop will continue executing until the
user externally interrupts the program.
int count = 1;
while (count <= 25)
{
System.out.println(count);
count = count - 1;
}
19
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
20
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

More Related Content

PPT
Ch4_part1.ppt
PPT
C Programming Looping Statements For Students
PPT
Ch4_part1.ppt
PPTX
Introduction to Java Programming - Lecture 11.pptx
PDF
DSA 103 Object Oriented Programming :: Week 3
PPT
15-Loops.ppt
PPT
Programming loop
PPT
Java Programming: Loops
Ch4_part1.ppt
C Programming Looping Statements For Students
Ch4_part1.ppt
Introduction to Java Programming - Lecture 11.pptx
DSA 103 Object Oriented Programming :: Week 3
15-Loops.ppt
Programming loop
Java Programming: Loops

Similar to Lecture on repetition statements (loops) (20)

PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PDF
how to write loops in java explained vividly
PPTX
Looping statements
PPTX
Looping statements
PPT
Looping statements in Java
PPT
Data Structures and Algorithoms 05slide - Loops.ppt
PPT
Computer Programming, Loops using Java
PPT
Repetition Structure
PPT
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
PPTX
Java chapter 3
PPTX
130707833146508191
PPTX
Java loops for, while and do...while
PPT
CH5.ppt chatpter2 introduction dddto OOP
PPSX
Control structures
PPTX
Control statements in java
PDF
Java Repetiotion Statements
PDF
Week04
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
PPTX
Java covers syntax, data types, object-oriented concepts, control flow, excep...
PPT
9781439035665 ppt ch05
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
how to write loops in java explained vividly
Looping statements
Looping statements
Looping statements in Java
Data Structures and Algorithoms 05slide - Loops.ppt
Computer Programming, Loops using Java
Repetition Structure
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
Java chapter 3
130707833146508191
Java loops for, while and do...while
CH5.ppt chatpter2 introduction dddto OOP
Control structures
Control statements in java
Java Repetiotion Statements
Week04
Unit-02 Selection, Mathematical Functions and loops.pptx
Java covers syntax, data types, object-oriented concepts, control flow, excep...
9781439035665 ppt ch05
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Presentation on HIE in infants and its manifestations
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
01-Introduction-to-Information-Management.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
VCE English Exam - Section C Student Revision Booklet
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Presentation on HIE in infants and its manifestations
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharma ospi slides which help in ospi learning
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
A systematic review of self-coping strategies used by university students to ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Structure & Organelles in detailed.
01-Introduction-to-Information-Management.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Ad

Lecture on repetition statements (loops)

  • 2. 2 Loops – While, Do, For • Repetition Statements – While – Do – For
  • 3. 3 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
  • 4. 4 The while Statement • A while statement has the following syntax: • 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 while ( condition ) statement;
  • 5. Logic of a while Loop statement true false condition evaluated 5
  • 6. 6 The while Statement • An example of a while statement: • 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 int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; }
  • 7. Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Initialize count animation 7
  • 8. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is true animation 8
  • 9. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java animation 9
  • 10. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 1 now animation 10
  • 11. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is still true since count is 1 animation 11
  • 12. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java animation 12
  • 13. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 2 now animation 13
  • 14. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is false since count is 2 now animation 14
  • 15. Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } The loop exits. Execute the next statement after the loop. animation 15
  • 17. 17 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
  • 18. 18 Infinite Loops • An example of an infinite loop: • This loop will continue executing until the user externally interrupts the program. int count = 1; while (count <= 25) { System.out.println(count); count = count - 1; }
  • 19. 19 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
  • 20. 20 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