SlideShare una empresa de Scribd logo
1 de 63
Loops
Motivations ,[object Object],[object Object],[object Object]
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
while  Loop Flow Chart while (loop-continuation-condition) { // loop-body; Statement(s); } int count = 0; while (count < 100) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Loop Continuation  Condition? true Statement(s) (loop body) false (count < 100)? true System.out.println(&quot;Welcome to Java!&quot;); count++; false ( A ) ( B ) count = 0;
Trace while Loop int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Initialize count animation count = 0
Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } (count < 2) is true animation count = 0
Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Print Welcome to Java animation count = 0
Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Increase count by 1 count is 1 now animation count = 1
Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } (count < 2) is still true since count is 1 animation count = 1
Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Print Welcome to Java animation count = 1
Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Increase count by 1 count is 2 now animation count = 2
Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } (count < 2) is false since count is 2 now animation count = 2
Trace while Loop int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } The loop exits. Execute the next statement after the loop. animation count = 2
Problem: Guessing Numbers   ,[object Object]
GuessNumberOneTime.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
GuessNumber.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Problem: An Advanced Math Learning Tool   ,[object Object]
SubtractionQuizLoop.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
SubtractionQuizLoop.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
SubtractionQuizLoop.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ending a Loop with a Sentinel Value  ,[object Object],[object Object]
SentinelValue.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Caution ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
do-while  Loop do { // Loop body; Statement(s); } while (loop-continuation-condition);
do-while  Loop ,[object Object],[object Object],[object Object],[object Object],[object Object]
for  Loops ,[object Object],[object Object],[object Object],[object Object],int i; for (i = 0; i < 100; i++) {   System.out.println( &quot;Welcome to Java!&quot;);  }
Trace for Loop int i; for (i = 0; i < 2; i++) {   System.out.println( &quot;Welcome to Java!&quot;);  } Declare i animation i =
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println( &quot;Welcome to Java!&quot;);  } Execute initializer i is now 0 animation i =  0
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println( &quot;Welcome to Java!&quot;);  } (i < 2) is true  since i is 0 animation i =  0
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println(&quot;Welcome to Java!&quot;);  } Print Welcome to Java animation
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println(&quot;Welcome to Java!&quot;);  } Execute adjustment statement  i now is 1 animation i =  1
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println(&quot;Welcome to Java!&quot;);  } (i < 2) is still true  since i is 1 animation i =  1
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println(&quot;Welcome to Java!&quot;);  } Print Welcome to Java animation i =  1
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println(&quot;Welcome to Java!&quot;);  } Execute adjustment statement  i now is 2 animation i =  2
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println(&quot;Welcome to Java!&quot;);  } (i < 2) is false  since i is 2 animation i =  2
Trace for Loop, cont. int i; for (i = 0; i < 2; i++) {   System.out.println(&quot;Welcome to Java!&quot;);  } Exit the loop. Execute the next statement after the loop animation i =  2
Note ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Note If the  loop-continuation-condition  in a  for  loop is omitted, it is implicitly true. Thus the statement given below in (a), which is an infinite loop, is correct. Nevertheless, it is better to use the equivalent loop in (b) to avoid confusion:
Caution ,[object Object],Logic Error for (int i=0; i<10; i++); { System.out.println(&quot;i is &quot; + i); }
Caution, cont. ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Logic Error Correct
Which Loop to Use? The three forms of loop statements,  while ,  do-while , and  for , are expressively equivalent; that is, you can write a loop in any of these three forms.   For example, a  while  loop in (a) in the following figure can always be converted into the following  for  loop in (b): A for loop in (a) in the following figure can generally be converted into the following while loop in (b) except in certain special cases (see Review Question 3.19 for one of them):
Recommendations Use the one that is most intuitive and comfortable for you. In general, a for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.
while, do while, for Loops ,[object Object],[object Object],[object Object],[object Object]
Nested Loops  ,[object Object]
MultiplicationTable.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Minimizing Numerical Errors  ,[object Object],[object Object],TestSum
TestSum.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
TestSum2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Problem: Finding the Greatest Common Divisor   Problem:  Write a program that prompts the user to enter two positive integers and finds their greatest common divisor.   Solution:  Suppose you enter two integers 4 and 2, their greatest common divisor is 2. Suppose you enter two integers 16 and 24, their greatest common divisor is 8. So, how do you find the greatest common divisor? Let the two input integers be  n1  and  n2 . You know number 1 is a common divisor, but it may not be the greatest commons divisor. So you can check whether  k  (for  k  = 2, 3, 4, and so on) is a common divisor for  n1  and  n2 , until  k  is greater than  n1  or  n2 .
GreatestCommonDivisor.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Problem: Finding the Sales Amount ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
FindSalesAmount.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Problem: Displaying a Pyramid of Numbers Problem: Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid. For example, if the input integer is 12, the output is shown below.
PrintPyramid.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using  break  and  continue Examples for using the  break  and  continue  keywords: break : immediately ends the innermost loop that contains it.  In other words,  break  breaks out of a loop. continue : ends only the current iteration.  Program control goes to the end of the loop body.  In other words,  continue  breaks out of an iteration. ,[object Object],[object Object]
TestBreak.java and TestContinue.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],public class TestContinue {  public static void main(String[] args) {  int sum = 0;  int number = 0;  while (number < 20) {  number++;  if (number == 10 || number == 11) continue;  sum += number;  }  System.out.println(&quot;The sum is &quot; + sum);  }  }
Guessing Number Problem Revisited   ,[object Object]
GuessNumberUsingBreak.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Problem: Displaying Prime Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PrimeNumbers.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PrimeNumbers.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
(GUI) Controlling a Loop with a Confirmation Dialog  A sentinel-controlled loop can be implemented using a confirmation dialog. The answers  Yes  or  No  to continue or terminate the loop. The template of the loop may look as follows: int  option = 0; while  (option == JOptionPane.YES_OPTION) { System.out.println(&quot;continue loop&quot;); option = JOptionPane.showConfirmDialog( null , &quot;Continue?&quot;); }
SentinelValueUsingConfirmationDialog.java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in pythonnitamhaske
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38myrajendra
 
Ch 3 event driven programming
Ch 3 event driven programmingCh 3 event driven programming
Ch 3 event driven programmingChaffey College
 
Event handling63
Event handling63Event handling63
Event handling63myrajendra
 
Unang Yugto ng Kolonyalismo at Imperyalismong Kaunlarin
Unang Yugto ng Kolonyalismo at Imperyalismong KaunlarinUnang Yugto ng Kolonyalismo at Imperyalismong Kaunlarin
Unang Yugto ng Kolonyalismo at Imperyalismong Kaunlarinjennilynagwych
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controlsGuddu gupta
 
Java Networking
Java NetworkingJava Networking
Java NetworkingSunil OS
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Languagejaimefrozr
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Why Task Queues - ComoRichWeb
Why Task Queues - ComoRichWebWhy Task Queues - ComoRichWeb
Why Task Queues - ComoRichWebBryan Helmig
 
Comparing Cpp And Erlang For Motorola Telecoms Software
Comparing Cpp And Erlang For Motorola Telecoms SoftwareComparing Cpp And Erlang For Motorola Telecoms Software
Comparing Cpp And Erlang For Motorola Telecoms Softwarel xf
 

La actualidad más candente (20)

Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Inter threadcommunication.38
Inter threadcommunication.38Inter threadcommunication.38
Inter threadcommunication.38
 
Ch 3 event driven programming
Ch 3 event driven programmingCh 3 event driven programming
Ch 3 event driven programming
 
Java threading
Java threadingJava threading
Java threading
 
Imperyalismo
ImperyalismoImperyalismo
Imperyalismo
 
Event handling63
Event handling63Event handling63
Event handling63
 
Unang Yugto ng Kolonyalismo at Imperyalismong Kaunlarin
Unang Yugto ng Kolonyalismo at Imperyalismong KaunlarinUnang Yugto ng Kolonyalismo at Imperyalismong Kaunlarin
Unang Yugto ng Kolonyalismo at Imperyalismong Kaunlarin
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Java exception
Java exception Java exception
Java exception
 
Eventos e interactividad - Small Basic
Eventos e interactividad - Small BasicEventos e interactividad - Small Basic
Eventos e interactividad - Small Basic
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
C# Loops
C# LoopsC# Loops
C# Loops
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Why Task Queues - ComoRichWeb
Why Task Queues - ComoRichWebWhy Task Queues - ComoRichWeb
Why Task Queues - ComoRichWeb
 
Comparing Cpp And Erlang For Motorola Telecoms Software
Comparing Cpp And Erlang For Motorola Telecoms SoftwareComparing Cpp And Erlang For Motorola Telecoms Software
Comparing Cpp And Erlang For Motorola Telecoms Software
 

Similar a Ch5(loops)

04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...AhmadHashlamon
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the BasicsJussi Pohjolainen
 
The java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfThe java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfakaluza07
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Repetition Structure.pptx
Repetition Structure.pptxRepetition Structure.pptx
Repetition Structure.pptxrhiene05
 
Psuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptxPsuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptxMattFlordeliza1
 
Bti1022 lab sheet 7
Bti1022 lab sheet 7Bti1022 lab sheet 7
Bti1022 lab sheet 7alish sha
 
Java™ (OOP) - Chapter 4: "Loops"
Java™ (OOP) - Chapter 4: "Loops"Java™ (OOP) - Chapter 4: "Loops"
Java™ (OOP) - Chapter 4: "Loops"Gouda Mando
 

Similar a Ch5(loops) (20)

04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
The java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfThe java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdf
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Repetition Structure.pptx
Repetition Structure.pptxRepetition Structure.pptx
Repetition Structure.pptx
 
Psuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptxPsuedocode1, algorithm1, Flowchart1.pptx
Psuedocode1, algorithm1, Flowchart1.pptx
 
Algorithms with-java-1.0
Algorithms with-java-1.0Algorithms with-java-1.0
Algorithms with-java-1.0
 
Java programs
Java programsJava programs
Java programs
 
Comp102 lec 6
Comp102   lec 6Comp102   lec 6
Comp102 lec 6
 
Bti1022 lab sheet 7
Bti1022 lab sheet 7Bti1022 lab sheet 7
Bti1022 lab sheet 7
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Java™ (OOP) - Chapter 4: "Loops"
Java™ (OOP) - Chapter 4: "Loops"Java™ (OOP) - Chapter 4: "Loops"
Java™ (OOP) - Chapter 4: "Loops"
 

Último

4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 

Último (20)

4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 

Ch5(loops)

  • 2.
  • 3.
  • 4. while Loop Flow Chart while (loop-continuation-condition) { // loop-body; Statement(s); } int count = 0; while (count < 100) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Loop Continuation Condition? true Statement(s) (loop body) false (count < 100)? true System.out.println(&quot;Welcome to Java!&quot;); count++; false ( A ) ( B ) count = 0;
  • 5. Trace while Loop int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Initialize count animation count = 0
  • 6. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } (count < 2) is true animation count = 0
  • 7. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Print Welcome to Java animation count = 0
  • 8. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Increase count by 1 count is 1 now animation count = 1
  • 9. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } (count < 2) is still true since count is 1 animation count = 1
  • 10. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Print Welcome to Java animation count = 1
  • 11. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } Increase count by 1 count is 2 now animation count = 2
  • 12. Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } (count < 2) is false since count is 2 now animation count = 2
  • 13. Trace while Loop int count = 0; while (count < 2) { System.out.println(&quot;Welcome to Java!&quot;); count++; } The loop exits. Execute the next statement after the loop. animation count = 2
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24. do-while Loop do { // Loop body; Statement(s); } while (loop-continuation-condition);
  • 25.
  • 26.
  • 27. Trace for Loop int i; for (i = 0; i < 2; i++) { System.out.println( &quot;Welcome to Java!&quot;); } Declare i animation i =
  • 28. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println( &quot;Welcome to Java!&quot;); } Execute initializer i is now 0 animation i = 0
  • 29. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println( &quot;Welcome to Java!&quot;); } (i < 2) is true since i is 0 animation i = 0
  • 30. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println(&quot;Welcome to Java!&quot;); } Print Welcome to Java animation
  • 31. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println(&quot;Welcome to Java!&quot;); } Execute adjustment statement i now is 1 animation i = 1
  • 32. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println(&quot;Welcome to Java!&quot;); } (i < 2) is still true since i is 1 animation i = 1
  • 33. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println(&quot;Welcome to Java!&quot;); } Print Welcome to Java animation i = 1
  • 34. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println(&quot;Welcome to Java!&quot;); } Execute adjustment statement i now is 2 animation i = 2
  • 35. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println(&quot;Welcome to Java!&quot;); } (i < 2) is false since i is 2 animation i = 2
  • 36. Trace for Loop, cont. int i; for (i = 0; i < 2; i++) { System.out.println(&quot;Welcome to Java!&quot;); } Exit the loop. Execute the next statement after the loop animation i = 2
  • 37.
  • 38. Note If the loop-continuation-condition in a for loop is omitted, it is implicitly true. Thus the statement given below in (a), which is an infinite loop, is correct. Nevertheless, it is better to use the equivalent loop in (b) to avoid confusion:
  • 39.
  • 40.
  • 41. Which Loop to Use? The three forms of loop statements, while , do-while , and for , are expressively equivalent; that is, you can write a loop in any of these three forms. For example, a while loop in (a) in the following figure can always be converted into the following for loop in (b): A for loop in (a) in the following figure can generally be converted into the following while loop in (b) except in certain special cases (see Review Question 3.19 for one of them):
  • 42. Recommendations Use the one that is most intuitive and comfortable for you. In general, a for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49. Problem: Finding the Greatest Common Divisor Problem: Write a program that prompts the user to enter two positive integers and finds their greatest common divisor. Solution: Suppose you enter two integers 4 and 2, their greatest common divisor is 2. Suppose you enter two integers 16 and 24, their greatest common divisor is 8. So, how do you find the greatest common divisor? Let the two input integers be n1 and n2 . You know number 1 is a common divisor, but it may not be the greatest commons divisor. So you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2 , until k is greater than n1 or n2 .
  • 50.
  • 51.
  • 52.
  • 53. Problem: Displaying a Pyramid of Numbers Problem: Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid. For example, if the input integer is 12, the output is shown below.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62. (GUI) Controlling a Loop with a Confirmation Dialog A sentinel-controlled loop can be implemented using a confirmation dialog. The answers Yes or No to continue or terminate the loop. The template of the loop may look as follows: int option = 0; while (option == JOptionPane.YES_OPTION) { System.out.println(&quot;continue loop&quot;); option = JOptionPane.showConfirmDialog( null , &quot;Continue?&quot;); }
  • 63.