200 likes | 304 Views
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
E N D
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.
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");
Infinite loops in Java (2 == 2);
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");
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");
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"
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"
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");
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("*****");
Pseudocode • // Program that displays squares and cubes • // Initialise • // Display the heading • do { • // Display the square and cube • } while (condition); • // Display end of job message
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");
……... • // 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");
// 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);
// 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");
Checking for Valid Input p136 • do { • System.out.println("Please enter a quantity (> 0): "); • qty = Keyboard.readInt(); • }while (qty <= 0);
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);
// 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);
Homework • Explorer’s guide Chapter 6 pp121– 138 • Pay particular attention to Exercise 5