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

C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
The Hunger Games - Context
The Hunger Games - ContextThe Hunger Games - Context
The Hunger Games - Contextjulier3846
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservationSwarup Kumar Boro
 
English2.9(definition of epic poem)
English2.9(definition of epic poem)English2.9(definition of epic poem)
English2.9(definition of epic poem)Eemlliuq Agalalan
 
Python for kids - 1.pptx
Python for kids - 1.pptxPython for kids - 1.pptx
Python for kids - 1.pptxsanyam1427
 
11 Unit 1 Problem Solving Techniques
11  Unit 1 Problem Solving Techniques11  Unit 1 Problem Solving Techniques
11 Unit 1 Problem Solving TechniquesPraveen M Jigajinni
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
Smart modeling of smart software
Smart modeling of smart softwareSmart modeling of smart software
Smart modeling of smart softwareJordi Cabot
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and OperatorsSunil OS
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition StructurePRN USM
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
Java: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh EditionJava: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh Editionmoxuji
 

La actualidad más candente (20)

C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
The Hunger Games - Context
The Hunger Games - ContextThe Hunger Games - Context
The Hunger Games - Context
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
English2.9(definition of epic poem)
English2.9(definition of epic poem)English2.9(definition of epic poem)
English2.9(definition of epic poem)
 
Python for kids - 1.pptx
Python for kids - 1.pptxPython for kids - 1.pptx
Python for kids - 1.pptx
 
History of c++
History of c++ History of c++
History of c++
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
 
11 Unit 1 Problem Solving Techniques
11  Unit 1 Problem Solving Techniques11  Unit 1 Problem Solving Techniques
11 Unit 1 Problem Solving Techniques
 
C presentation
C presentationC presentation
C presentation
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Python-Functions.pptx
Python-Functions.pptxPython-Functions.pptx
Python-Functions.pptx
 
Smart modeling of smart software
Smart modeling of smart softwareSmart modeling of smart software
Smart modeling of smart software
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Java: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh EditionJava: The Complete Reference, Eleventh Edition
Java: The Complete Reference, Eleventh Edition
 
Python
PythonPython
Python
 

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

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Último (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

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.