SlideShare ist ein Scribd-Unternehmen logo
1 von 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]

Weitere ähnliche Inhalte

Was ist angesagt?

Living in the IT Era - Lesson 9.pptx
Living in the IT Era - Lesson 9.pptxLiving in the IT Era - Lesson 9.pptx
Living in the IT Era - Lesson 9.pptxFroilan Cantillo
 
Pipeline Mechanism
Pipeline MechanismPipeline Mechanism
Pipeline MechanismAshik Iqbal
 
HCI - Chapter 2
HCI - Chapter 2HCI - Chapter 2
HCI - Chapter 2Alan Dix
 
Computerized grading system chapter 1-3 ( summarization )
Computerized grading system chapter 1-3 ( summarization )Computerized grading system chapter 1-3 ( summarization )
Computerized grading system chapter 1-3 ( summarization )Chriselle24
 
HCI 3e - Ch 20: Ubiquitous computing and augmented realities
HCI 3e - Ch 20:  Ubiquitous computing and augmented realitiesHCI 3e - Ch 20:  Ubiquitous computing and augmented realities
HCI 3e - Ch 20: Ubiquitous computing and augmented realitiesAlan Dix
 
87683689 ooad-lab-record
87683689 ooad-lab-record87683689 ooad-lab-record
87683689 ooad-lab-recordPon Venkatesh
 
TRAIN TICKETING SYSTEM
TRAIN TICKETING SYSTEMTRAIN TICKETING SYSTEM
TRAIN TICKETING SYSTEMNimRaH NaZaR
 
Airline Reservation System
Airline Reservation SystemAirline Reservation System
Airline Reservation SystemArohi Khandelwal
 
Thesis in Computerized Payroll System for Brangay Hall, Dita
Thesis in Computerized Payroll System for Brangay Hall, DitaThesis in Computerized Payroll System for Brangay Hall, Dita
Thesis in Computerized Payroll System for Brangay Hall, DitaAcel Carl David O, Dolindo
 
Student Monitoring Attendance System
Student Monitoring Attendance SystemStudent Monitoring Attendance System
Student Monitoring Attendance Systemyumico23
 
Implementing security
Implementing securityImplementing security
Implementing securityDhani Ahmad
 

Was ist angesagt? (15)

Living in the IT Era - Lesson 9.pptx
Living in the IT Era - Lesson 9.pptxLiving in the IT Era - Lesson 9.pptx
Living in the IT Era - Lesson 9.pptx
 
Pipeline Mechanism
Pipeline MechanismPipeline Mechanism
Pipeline Mechanism
 
HCI - Chapter 2
HCI - Chapter 2HCI - Chapter 2
HCI - Chapter 2
 
Computerized grading system chapter 1-3 ( summarization )
Computerized grading system chapter 1-3 ( summarization )Computerized grading system chapter 1-3 ( summarization )
Computerized grading system chapter 1-3 ( summarization )
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
 
College space
College spaceCollege space
College space
 
HCI 3e - Ch 20: Ubiquitous computing and augmented realities
HCI 3e - Ch 20:  Ubiquitous computing and augmented realitiesHCI 3e - Ch 20:  Ubiquitous computing and augmented realities
HCI 3e - Ch 20: Ubiquitous computing and augmented realities
 
87683689 ooad-lab-record
87683689 ooad-lab-record87683689 ooad-lab-record
87683689 ooad-lab-record
 
TRAIN TICKETING SYSTEM
TRAIN TICKETING SYSTEMTRAIN TICKETING SYSTEM
TRAIN TICKETING SYSTEM
 
Airline Reservation System
Airline Reservation SystemAirline Reservation System
Airline Reservation System
 
Thesis in Computerized Payroll System for Brangay Hall, Dita
Thesis in Computerized Payroll System for Brangay Hall, DitaThesis in Computerized Payroll System for Brangay Hall, Dita
Thesis in Computerized Payroll System for Brangay Hall, Dita
 
Attendance monitoring system
Attendance monitoring systemAttendance monitoring system
Attendance monitoring system
 
Student Monitoring Attendance System
Student Monitoring Attendance SystemStudent Monitoring Attendance System
Student Monitoring Attendance System
 
Implementing security
Implementing securityImplementing security
Implementing security
 
Cloud computing ppts
Cloud computing pptsCloud computing ppts
Cloud computing ppts
 

Ähnlich wie 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
 

Ähnlich wie 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"
 

Kürzlich hochgeladen

Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
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
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
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
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
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.
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 

Kürzlich hochgeladen (20)

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
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
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
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
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
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
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...
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 

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.