Full Transcript

Iterative Control Structures do – while loop while() • while statement - test for continuation of the loop is carried out at the beginning of each pass through the loop • do-while statement - the test for continuation is at the end of each pass • This guarantees statement will always be executed a...

Iterative Control Structures do – while loop while() • while statement - test for continuation of the loop is carried out at the beginning of each pass through the loop • do-while statement - the test for continuation is at the end of each pass • This guarantees statement will always be executed at least once unlike the while loop which may never execute do/while Flowchart Statement; Control condition false true do/while loop 1. Execute the statements in the loop 2. Evaluate the control condition 3. When the control condition is true go back to step 1 4. When the control condition is false exit the loop and execute the next statement after the loop do-while loop General Form do { statements; }while (expression); • Repeats the statement or statements while the expression is true • Code will always be executed at least once while vs do-while • A do-while will execute at least once • A while may never execute • A while executes zero or more times • A do-while executes one or more times do while Flowchart counter =1 System.out.print(“Welcome to Java”); counter++; counter<=100 true Terminates when counter is 101 false Sample Program 1 //program to display text on screen 100 times public class DoWhile1 { public static void main(String args[]) { //declare variables int counter = 1; //declare variable and initialize with 1 do { //display text System.out.println(“Welcome to Java"); counter++; //add 1 to counter using increment operator } while(counter <= 100); //check condition }//end main method }//end class Note use of ; at end of condition Sample Program 2 //program to display digits 0,1,2,3,…9 public class DoWhile2 { public static void main(String args[]) { //declare variables int counter = 0; //declare variable and initialize with 0 do { System.out.println(counter); //display value of counter counter++; //add 1 to counter } while(counter <= 9); //check condition }//end main method }//end class Sample Program 3 //total numbers from 1 to 10 public class DoWhile3 { public static void main(String args []) { int counter =1, total = 0; do { total = total + counter; //add counter to total each time round counter++; //increment counter } while(counter <= 10) //Print total outside the loop System.out.println("Total of numbers from 1 to 10 is " +total); }//end main method }//end class do while • do while loop can also be used as counter controlled loop or sentinel loop • The difference between a while loop and do while loop is the order in which the loop condition is evaluated and the loop body is executed • Which loop to choose usually depends on the problem the program presents Sample Program 4 - Sentinel • Continue entering a number while that number is positive • Amount of positive numbers to be entered is unknown • Negative number is the sentinel value //continue entering a positive number import java.util.*; public class NumberSentinel { public static void main(String args[]) { Scanner keyboardIn = new Scanner(System.in); //declare variable int number; do { //Get User to enter number System.out.print("Enter a positive number - neg number will exit the loop:"); number = keyboardIn.nextInt(); }while (number >= 0);//continue repeating if user enters positive num System.out.print(number + " terminated the loop"); } //end main } //end class

Use Quizgecko on...
Browser
Browser