1 / 20

415.111 Monday 21 st May Java Lecture 8 Overview

415.111 Monday 21 st May Java Lecture 8 Overview. Explorer’s Guide Chapter 6 The do-while loop Infinite loops Looping while a condition is true Loops that are executed a pre-determined number of times. int cost, payment, change; Ch 5 Ex 9 // Input

elisha
Download Presentation

415.111 Monday 21 st May Java Lecture 8 Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 415.111 Monday 21st MayJava Lecture 8 Overview • Explorer’s Guide Chapter 6 • The do-while loop • Infinite loops • Looping while a condition is true • Loops that are executed a pre-determined number of times.

  2. int cost, payment, change; Ch 5 Ex 9 // Input System.out.print("Please enter the cost: "); cost = Keyboard.readInt(); System.out.print("Please enter the payment made: "); payment = Keyboard.readInt(); // Process change = payment - cost; // Output if (change > 0) System.out.println("Change is $" + change); else if (change < 0) System.out.println("Amount owing is $" + -change); else if (change == 0) System.out.println("Thank you");

  3. Infinite loops in Java (2 == 2);

  4. The PasswordChecker program p122 • // Loop until an exit condition is met • int password; • do { • System.out.print("Please enter your password: "); • password = Keyboard.readInt(); • } while (password != 9876); • System.out.print("System access granted");

  5. Looping while a condition is true

  6. Loops that are Executed a Certain Number of Times • In Chapter 1: • Can now use a loop • Plus a counter • // Says hello to the Kiwi three times. • System.out.println(); • System.out.println("Hello Kiwi"); • System.out.println("Hello Kiwi"); • System.out.println("Hello Kiwi");

  7. In Pseudocode: • // Program to display Hello Kiwi three times • // using a loop • int timesExecutedSoFar; • timesExecutedSoFar = 0; • // A loop to output 'Hello Kiwi' three times • // Display "You win"

  8. The Java program: • // Says hello to the Kiwi three times using a loop. • int timesExecutedSoFar; • timesExecutedSoFar = 0; • // A loop that is executed exactly three times • do { • System.out.println("Hello Kiwi"); • timesExecutedSoFar = timesExecutedSoFar + 1; • } while (a condition to stop the loop at the right time) • // Display "You win"

  9. In Java • // Says hello to the Kiwi three times using a loop. • int timesExecutedSoFar; • timesExecutedSoFar = 0; • // A loop that is executed exactly three times • do { • System.out.println("Hello Kiwi"); • timesExecutedSoFar = timesExecutedSoFar + 1; • } while (timesExecutedSoFar != 3); • System.out.println("You win");

  10. Ex 2 What is the Output? E. G. p128 • int counter; • counter = 0; • do { • System.out.println("*"); • counter = counter + 1; • } while (counter != 4); • System.out.println("*****");

  11. Finding Squares

  12. Pseudocode • // Program that displays squares and cubes • // Initialise • // Display the heading • do { • // Display the square and cube • } while (condition); • // Display end of job message

  13. Level 2: • // Program that displays squares and cubes • int counter; • // Initialise • counter = 1; • // Display the heading • do { • // Display the square and cube • counter = counter + 2; • } while (condition); • // Display end of job message • System.out.println("Job completed");

  14. ……... • // Display the heading • System.out.println("Counter " + "Square " + "Cube "); • do { • // Display the value of Counter • System.out.print(counter + " "); • // Display the square of Counter } • System.out.print(counter * counter + " "); • // Display the cube of Counter } • System.out.println(counter * counter * counter); • counter = counter + 2; • } while (counter ??? 10); • // Display end of job message } • System.out.println("Job completed");

  15. // Program to add successive inputs together. p134 int deposit, total; total = 0; 1 System.out.println(); System.out.print("Please enter first deposit: "); deposit = Keyboard.readInt(); total = total + deposit; 2 System.out.print("Please enter another deposit: "); deposit = Keyboard.readInt(); total = total + deposit; System.out.println("Total is " + total); 3 System.out.print("Please enter another deposit: "); deposit = Keyboard.readInt(); total = total + deposit; System.out.println("Total is " + total);

  16. // Program to add successive inputs together (using a loop) int deposit, total; char yOrN;   total = 0; System.out.print("Please enter a Deposit: "); deposit = Keyboard.readInt(); total = total + deposit; do { System.out.print("Please enter another Deposit: "); deposit = Keyboard.readInt(); total = total + deposit; System.out.print("Total is " + total); System.out.println(); System.out.print("Finished (y or n)? "); yOrN = Keyboard.readChar(); } while (yOrN == 'n' || yOrN == 'N'); System.out.println("End of Job");

  17. Checking for Valid Input p136 • do { • System.out.println("Please enter a quantity (> 0): "); • qty = Keyboard.readInt(); • }while (qty <= 0);

  18. Checking for Valid Input p136 • do { • System.out.println("Enter number per pack (1, 6 or 12): "); • nbrPerPack = Keyboard.readInt(); • } while (nbrPerPack != 1 && nbrPerPack != 6 && • nbrPerPack != 12);

  19. // Display a label of limited length p137 final String dashes = "--------------------"; String locker; do { System.out.println("Please enter name and locker number"); System.out.println("Less than 20 characters: "); locker = Keyboard.readString(); } while (locker.length() >= 20); System.out.println(dashes); System.out.println(locker); System.out.println(dashes);

  20. Homework • Explorer’s guide Chapter 6 pp121– 138 • Pay particular attention to Exercise 5

More Related