SlideShare a Scribd company logo
1 of 23
Loops
Repetition Statements
Repetition Statements
• Repetition statements allow us to execute a
statement multiple times
• Often they are referred to as loops
• Like conditional statements, they are
controlled by boolean expressions
Repetition Statements
• Java has three kinds of repetition statements:
 the while loop
 the do loop
 the for loop
• The programmer should choose the right kind of
loop for the situation
4
The while Statement syntax
• A while statement has the following syntax:
while ( condition ){
statements;
}
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
Logic of a while Loop
statement
true false
condition
evaluated
Example 1:
• An example of a while statement:
int count = 1;
while (count <= 3){
System.out.println (count);
count++;
}
Example 2
• An example of a while statement:
int count = 1;
while (count < 2){
System.out.println (count);
count++;
}
Example 3
• An example of a while statement:
int count = 7;
while (count < 7){
System.out.println (count);
count++;
}
Example 4
• An example of a while statement:
int count = 2;
while (count < 3){
System.out.println (count);
}
10
Infinite Loops
• The body of a while loop eventually must 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
normally Java/loops/While1.java
WhileEx2.java
WhileEx1.java
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
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
The do Statement
• A do statement has the following syntax:
do{
statement;
}
while ( condition )
The statement is executed once initially, and then the
condition is evaluated
The statement is executed repeatedly until the condition
becomes false
Logic of a do Loop
true
condition
evaluated
statement
false
The do Statement
• An example of a do loop:
int count = 0;
do{
count++;
System.out.println (count);
} while (count < 3);
The do Statement
• An example of a do loop:
int count = 100;
do{
count++;
System.out.println (count);
} while (count < 3);
The body of a do loop executes at least once
Comparing while and do
statement
true false
condition
evaluated
The while Loop
true
condition
evaluated
statement
false
The do Loop
The for Statement
• A for statement has the following syntax:
for ( initialization ; condition ; update ){
statements;
}
The initialization
is executed once
before the loop begins
The statement is
executed until the
condition becomes false
The increment portion is executed at
the end of each iteration
Logic of a for loop
statements
true
condition
evaluated
false
update
initialization
The for Statement
• A for loop is functionally equivalent to the
following while loop structure:
initialization;
while ( condition ){
statement;
update;
}
The for Statement
• An example of a for loop:
for (int count=1; count <= 3; count++){
System.out.println (count);
}
The initialization section can be used to declare a variable
Like a while loop, the condition of a for loop is tested prior
to executing the loop body
Therefore, the body of a for loop will execute zero or more
times
The for Statement
• Each expression in the header of a for loop is
optional
• If the initialization is left out, no initialization is
performed
• If the condition is left out, it is always considered
to be true, and therefore creates an infinite loop
• If the increment is left out, no increment operation
is performed Java/Loops.For1.java
For2.java, For3.java, For4.java,
Questions???
Java/Loops/ForWhile.java
ForInsideWhile.java

More Related Content

Similar to 15-Loops.ppt

whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdfBasirKhan21
 
While loop
While loopWhile loop
While loopFeras_83
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion StatementsHuda Alameen
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Abou Bakr Ashraf
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptxNaumanRasheed11
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Digvijaysinh Gohil
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 

Similar to 15-Loops.ppt (20)

whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
While loop
While loopWhile loop
While loop
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Loops in c ii
Loops in c iiLoops in c ii
Loops in c ii
 
Loops in c ii
Loops in c iiLoops in c ii
Loops in c ii
 
Loops c++
Loops c++Loops c++
Loops c++
 
Control structure
Control structureControl structure
Control structure
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
M C6java6
M C6java6M C6java6
M C6java6
 
Introducing Swift v2.1
Introducing Swift v2.1Introducing Swift v2.1
Introducing Swift v2.1
 
Loops
LoopsLoops
Loops
 

Recently uploaded

Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...ranjana rawat
 
Jp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile Service
Jp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile ServiceJp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile Service
Jp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile ServiceHigh Profile Call Girls
 
The Billo Photo Gallery - Cultivated Cuisine T1
The Billo Photo Gallery - Cultivated Cuisine T1The Billo Photo Gallery - Cultivated Cuisine T1
The Billo Photo Gallery - Cultivated Cuisine T1davew9
 
Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)Mohamed Miyir
 
Call Girl Nashik Khushi 7001305949 Independent Escort Service Nashik
Call Girl Nashik Khushi 7001305949 Independent Escort Service NashikCall Girl Nashik Khushi 7001305949 Independent Escort Service Nashik
Call Girl Nashik Khushi 7001305949 Independent Escort Service Nashikranjana rawat
 
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...Call Girls in Nagpur High Profile
 
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCyLet Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCystephieert
 
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...srsj9000
 
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Roomdivyansh0kumar0
 
BPP NC II Lesson 3 - Pastry Products.pptx
BPP NC II Lesson 3 - Pastry Products.pptxBPP NC II Lesson 3 - Pastry Products.pptx
BPP NC II Lesson 3 - Pastry Products.pptxmaricel769799
 
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...Suhani Kapoor
 
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service Bikaner
VIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service BikanerVIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service Bikaner
VIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service BikanerSuhani Kapoor
 
(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Service
(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Service(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Service
(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
4th QT WEEK 2 Cook Meat Cuts part 2.pptx
4th QT WEEK 2 Cook Meat Cuts part 2.pptx4th QT WEEK 2 Cook Meat Cuts part 2.pptx
4th QT WEEK 2 Cook Meat Cuts part 2.pptxKattieAlisonMacatugg1
 
(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...ranjana rawat
 

Recently uploaded (20)

Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...
Book Paid Chakan Call Girls Pune 8250192130Low Budget Full Independent High P...
 
Jp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile Service
Jp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile ServiceJp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile Service
Jp Nagar Call Girls Bangalore WhatsApp 8250192130 High Profile Service
 
The Billo Photo Gallery - Cultivated Cuisine T1
The Billo Photo Gallery - Cultivated Cuisine T1The Billo Photo Gallery - Cultivated Cuisine T1
The Billo Photo Gallery - Cultivated Cuisine T1
 
Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)
 
Call Girl Nashik Khushi 7001305949 Independent Escort Service Nashik
Call Girl Nashik Khushi 7001305949 Independent Escort Service NashikCall Girl Nashik Khushi 7001305949 Independent Escort Service Nashik
Call Girl Nashik Khushi 7001305949 Independent Escort Service Nashik
 
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort serviceyoung Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
 
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
 
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
 
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCyLet Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
 
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
Best Connaught Place Call Girls Service WhatsApp -> 9999965857 Available 24x7...
 
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With RoomVIP Kolkata Call Girl Jadavpur 👉 8250192130  Available With Room
VIP Kolkata Call Girl Jadavpur 👉 8250192130 Available With Room
 
BPP NC II Lesson 3 - Pastry Products.pptx
BPP NC II Lesson 3 - Pastry Products.pptxBPP NC II Lesson 3 - Pastry Products.pptx
BPP NC II Lesson 3 - Pastry Products.pptx
 
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
9953330565 Low Rate Call Girls In Sameypur-Bodli Delhi NCR
 
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
 
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
VIP Call Girls Service Shamshabad Hyderabad Call +91-8250192130
 
VIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service Bikaner
VIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service BikanerVIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service Bikaner
VIP Call Girl Bikaner Aashi 8250192130 Independent Escort Service Bikaner
 
Call Girls In Ramesh Nagar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Ramesh Nagar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCeCall Girls In Ramesh Nagar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
Call Girls In Ramesh Nagar꧁❤ 🔝 9953056974🔝❤꧂ Escort ServiCe
 
(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Service
(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Service(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Service
(SUNAINA) Call Girls Alandi Road ( 7001035870 ) HI-Fi Pune Escorts Service
 
4th QT WEEK 2 Cook Meat Cuts part 2.pptx
4th QT WEEK 2 Cook Meat Cuts part 2.pptx4th QT WEEK 2 Cook Meat Cuts part 2.pptx
4th QT WEEK 2 Cook Meat Cuts part 2.pptx
 
(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(KRITIKA) Balaji Nagar Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
 

15-Loops.ppt

  • 2. Repetition Statements • Repetition statements allow us to execute a statement multiple times • Often they are referred to as loops • Like conditional statements, they are controlled by boolean expressions
  • 3. Repetition Statements • Java has three kinds of repetition statements:  the while loop  the do loop  the for loop • The programmer should choose the right kind of loop for the situation
  • 4. 4 The while Statement syntax • A while statement has the following syntax: while ( condition ){ statements; } 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
  • 5. Logic of a while Loop statement true false condition evaluated
  • 6. Example 1: • An example of a while statement: int count = 1; while (count <= 3){ System.out.println (count); count++; }
  • 7. Example 2 • An example of a while statement: int count = 1; while (count < 2){ System.out.println (count); count++; }
  • 8. Example 3 • An example of a while statement: int count = 7; while (count < 7){ System.out.println (count); count++; }
  • 9. Example 4 • An example of a while statement: int count = 2; while (count < 3){ System.out.println (count); }
  • 10. 10 Infinite Loops • The body of a while loop eventually must 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 normally Java/loops/While1.java WhileEx2.java WhileEx1.java
  • 11. 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
  • 12. 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
  • 13. The do Statement • A do statement has the following syntax: do{ statement; } while ( condition ) The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false
  • 14. Logic of a do Loop true condition evaluated statement false
  • 15. The do Statement • An example of a do loop: int count = 0; do{ count++; System.out.println (count); } while (count < 3);
  • 16. The do Statement • An example of a do loop: int count = 100; do{ count++; System.out.println (count); } while (count < 3); The body of a do loop executes at least once
  • 17. Comparing while and do statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop
  • 18. The for Statement • A for statement has the following syntax: for ( initialization ; condition ; update ){ statements; } The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration
  • 19. Logic of a for loop statements true condition evaluated false update initialization
  • 20. The for Statement • A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ){ statement; update; }
  • 21. The for Statement • An example of a for loop: for (int count=1; count <= 3; count++){ System.out.println (count); } The initialization section can be used to declare a variable Like a while loop, the condition of a for loop is tested prior to executing the loop body Therefore, the body of a for loop will execute zero or more times
  • 22. The for Statement • Each expression in the header of a for loop is optional • If the initialization is left out, no initialization is performed • If the condition is left out, it is always considered to be true, and therefore creates an infinite loop • If the increment is left out, no increment operation is performed Java/Loops.For1.java For2.java, For3.java, For4.java,