DOC PREVIEW
Flow of Control Loops

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 4Slide 24.1 Java Loop StatementsRepetition simplifies codingHow would you sum up 1 through 100?How about 1 through 100,00?The for Statementfor Deconstructed <Sum of 1-100>Application Deconstructed <SumNumbers.java>Application Deconstructed <SumNumbers.java>Recap: forSometimes you may not want to repeat an action at allSumming a list of numbersThe while Statementwhile Deconstructed <Sum of numeric list>Application Deconstructed <SumNumericList.java>Application Deconstructed <SumNumericList.java>Recap: whileAnd sometimes you may want to repeat an action at least onceSum numbers until you reach 100The do-while Statementdo-while Deconstructed <Sum to reach 100>Application Deconstructed <SumToReach.java>Application Deconstructed <SumToReach.java>Recap: do-whileThe for-each Statementfor-each Deconstructed <Cycle through an enumeration>Application Deconstructed <ForEachEnum.java>4.2 Programming with LoopsIn Class ExamplesIn Class ExamplesRecap: Repetition4.3 Graphic SupplementApplet Deconstructed <FallingLetters.java>Applet Deconstructed <FallingLetters.java>Applet Deconstructed <FallingLetters.java>Applet Deconstructed <FallingLetters.java>Chapter 4Ch 1 – Introduction toComputers and JavaFlow of ControlLoops1Chapter 44.1 Java Loop Statements4.2 Programming with Loops4.3 Graphics Supplement24.1Java LoopStatements3repeat...repeat...repeatRepetition simplifies coding4How would you sum up 1 through 100?5sum = 1 + 2 + 3 + ..... 100sum = 1sum += 2...sum += 100How about 1 through 100,00?6We need a more efficient wayto accomplish this task.One that can scale up easily!The for Statement7sum = 0num = 1sum += numnum <= 100 num > 100num++endfor Deconstructed<Sum of 1-100>int sum = 0;for (int num = 1; num <= 100; num++) { sum += num;}8Is this solution scalable?int sum = 0;for (int num = 1; num <= 10000; num++) { sum += num;}initial stepcondition stepupdate stepApplication Deconstructed<SumNumbers.java>package sumnumbers;import java.util.Scanner;public class SumNumbers { public static void main(String[] args) { int upperBound; int sumOfNumbers = 0; Scanner keyboard = new Scanner(System.in); System.out.println("I will add numbers from 1 to n."); System.out.print("Please enter n: "); upperBound = keyboard.nextInt();9Application Deconstructed<SumNumbers.java> for (int num = 1; num <= upperBound; num++) { sumOfNumbers += num; } //end for System.out.println(""); System.out.println("The sum of 1 to " + upperBound + " is " + sumOfNumbers); } //end main()} //end SumNumbers10Recap: forUse to repeat a fixed number of timesExcellent choice for counting problemsVery concise – all steps on one line11Sometimes you may not wantto repeat an action at all12Summing a list of numbers13First decide what ends the listLet's say that -1 will signal the end of the inputNow, could the list be empty?It could, if user enters -1 as the first numberThe while Statement14get numsum += numnum != -1 num == -1get numendwhile Deconstructed<Sum of numeric list>int num;int sumOfNumericList = 0;System.out.print("Enter number of -1 to quit: ");num = keyboard.nextInt();while(num != -1) { sumOfNumericList += num; System.out.print("Enter number of -1 to quit: "); num = keyboard.nextInt();}15initial stepcondition stepupdate stepApplication Deconstructed<SumNumericList.java>package sumnumericlist;import java.util.Scanner;public class SumNumericList { public static void main(String[] args) { int num; int sumOfNumericList = 0; Scanner keyboard = new Scanner(System.in);16 System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); while (num != -1) { sumOfNumericList += num; System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); } //end whileApplication Deconstructed<SumNumericList.java> System.out.println("The sum of the numbers is " + sumOfNumericList); } //end main()} //end SumNumericList17Recap: whileUse to repeat 0 or more timesNumber of iterations is unknown before handTest before execution18And sometimes you may wantto repeat an action at least once19Sum numbers until you reach 10020Add next number if sum is less than 100Stop when sum becomes greater than or equal to 100The do-while Statement21sum = 0get numsum < 100sum >= 100sum += numenddo-while Deconstructed<Sum to reach 100>int num;int sumToReach = 0;do { System.out.print("Enter number: "); num = keyboard.nextInt(); sumToReach += num;} while(sumToReach < 100);22initial stepcondition stepupdate stepApplication Deconstructed<SumToReach.java>package sumtoreach;import java.util.Scanner;public class SumToReach { public static void main(String[] args) { int num; int sumToReach = 0; Scanner keyboard = new Scanner(System.in); do { System.out.print("Enter a number: "); num = keyboard.nextInt(); sumToReach += num; } while(sumToReach < 100);23Application Deconstructed<SumToReach.java> System.out.println("The sum is " + sumToReach); } //end main()} //end SumToReach24Recap: do-whileUse to repeat 1 or more timesNumber of iterations is unknown before handTest after execution25The for-each Statement26Allows you to cycle through an enumeration or collection, in its entiretyfor-each Deconstructed<Cycle through an enumeration>enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES}27for ( Suit nextSuit : Suit.values() ) { System.out.print(nextSuit + " ");}Application Deconstructed<ForEachEnum.java>package foreachenum;public class ForEachEnum { enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } public static void main(String[] args) { for ( Suit nextSuit : Suit.values() ) { System.out.print(nextSuit + " "); } //end for System.out.println(""); } //end main()} //end ForEachEnum284.2ProgrammingwithLoops29In Class Examples30for: Count downUser enters upper bound, lower boundand step down. Program outputs list.while: Number reversalUser enters integer to reverse.Program displays reversed number.In Class Examples31while: Structure violation (implement in class)int num = 0;int count = 0;while (num != -1) { read number count number}Recap: RepetitionStrive to always choose the most appropriate loop for the taskEach loop has a purpose and a structureDo not violate either one324.3GraphicSupplement33Applet Deconstructed<FallingLetters.java>package fallingletters;import java.awt.Color;import javax.swing.JApplet;import java.awt.Graphics;public class FallingLetters extends JApplet { public static final int LETTER_COUNT = 100;


Flow of Control Loops

Download Flow of Control Loops
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Flow of Control Loops and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Flow of Control Loops 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?