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 object orineted programming looping .ppt (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

More from zeenatparveen24 (10)

PPT
object oriented programming concept .ppt
PPT
c++ control structure statements .ppt
PPTX
introduction to c++concept presentation.pptx
PPT
8 Normalization (1).ppt
PDF
Cunit3.pdf
PPTX
pointers.pptx
PPTX
COCOMO.pptx
PPTX
4 B-Coupling and Cohesion-1.pptx
PPTX
Unit II.pptx
PPTX
cunit1.pptx
object oriented programming concept .ppt
c++ control structure statements .ppt
introduction to c++concept presentation.pptx
8 Normalization (1).ppt
Cunit3.pdf
pointers.pptx
COCOMO.pptx
4 B-Coupling and Cohesion-1.pptx
Unit II.pptx
cunit1.pptx
Ad

Recently uploaded (20)

PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Project quality management in manufacturing
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Current and future trends in Computer Vision.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
PPT on Performance Review to get promotions
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Sustainable Sites - Green Building Construction
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Digital Logic Computer Design lecture notes
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Project quality management in manufacturing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Current and future trends in Computer Vision.pptx
573137875-Attendance-Management-System-original
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Internet of Things (IOT) - A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Safety Seminar civil to be ensured for safe working.
PPT on Performance Review to get promotions
Mechanical Engineering MATERIALS Selection
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Sustainable Sites - Green Building Construction
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Foundation to blockchain - A guide to Blockchain Tech
Digital Logic Computer Design lecture notes
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx

object orineted programming looping .ppt

  • 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