Week 4 Counter V Sentinel Loops PDF

Summary

This document explains counter-controlled and sentinel-controlled loops in programming. It provides examples of how to implement these loops, including sample code in Java and pseudocode.

Full Transcript

Counter Controlled vs Sentinel Controlled Loops Counter controlled repetition • A class of ten students take a test. The grades are in range 0 to 100. Determine the average mark for the test • The average is the sum of the grades divided by the number of students • Use counter controlled repetiti...

Counter Controlled vs Sentinel Controlled Loops Counter controlled repetition • A class of ten students take a test. The grades are in range 0 to 100. Determine the average mark for the test • The average is the sum of the grades divided by the number of students • Use counter controlled repetition to input the grades one at a time Counter controlled repetition • Use a variable called counter to specify the number of times a set of statements should execute • Repetition terminates when counter exceeds 10 • Normally we would use a for loop for this – but we have not looked at for loops yet • total is a variable used to accumulate the sum of a series of values. • counter is a variable used to count – in this case – used to count the number of grades entered – counter is used to control the number of repetitions Pseudocode SET total to zero SET counter to 1 WHILE counter is less than or equal to 10 INPUT next grade ADD the grade to the total ADD one to counter CALC average ( total divided by 10) DISPLAY average Sample program 1 – find average of 10 grades //program to calculate the average of 10 grades import java.util.*; public class AverageGrade { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); //declare variables int counter = 1, total = 0, grade; double average; //read in 10 grades while(counter <= 10) //while counter is less than 11 { System.out.print("Enter grade no " +counter +": " ); grade = keyboardIn.nextInt(); total = total + grade; ///add current grade to total counter++; } //end while //calc average average = (double)total/10; //note use of cast System.out.println("Average grade is } //end main } //end class " +average); Counter v Sentinel • In counter controlled repetition a control variable is used to count the number of times a group of instructions should be repeated • A sentinel value can be used to terminate the repetition when it is not known in advance how many times a set of statements will be repeated. • The sentinel value is used to indicate the end of the input data. Sentinel controlled repetition • Develop a class averaging program that will process an arbitrary number of grades each time the program is run • In this example there is no indication of how many grades are to be entered • One solution is to use a special value called a sentinel value to indicate “end of data entry”. Sentinel controlled repetition • User enters all grades and then enters the sentinel value to indicate that the last grade has been entered. • The sentinel value must be chosen so that it cannot be confused with an acceptable input value. • In this example grades will be in range 0 to 100 so –1 is an acceptable sentinel value pseudocode SET total to 0 SET counter to 0 INPUT first grade WHILE user has not entered sentinel ADD grade to running total ADD one to counter ///why do we still need counter? INPUT next grade (possibly sentinel) CALC average (total divided by counter) DISPLAY average //sentinal controlled loop import java.util.*; public class AverageGradeSentinalEg { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); //declare variables int counter = 0, total = 0, grade; double average; System.out.print("Enter grade no "+(counter+1) +": " ); grade = keyboardIn.nextInt(); //read in grades while(grade!= -1) { total = total + grade; ///add current grade to total counter++; //keep track of no of grades – WHY? System.out.print("Enter grade #"+(counter+1) +": " ); grade = keyboardIn.nextInt(); } //end while //calc average average = (double)total/counter; //note use of cast System.out.println("Average grade is } //end main } //end class " +average); Sentinel Example 2 • Use Sentinel value to allow a user to confirm if they want to continue playing a game or quit • Y to continue. N to quit • Counter controlled loop is no use as we don’t know how many games user wants to continue playing //sentinal controlled loop example 2 import java.util.*; public class GameSentinel { public static void main(String args[]) { Scanner keyboardIn = new Scanner(System.in); //declare variable char continueGame = 'Y'; while (continueGame == 'Y') //continue repeating if user enters { System.out.print("Enter Y to continue the game or N to quit:"); continueGame = keyboardIn.next().charAt(0); }//end while System.out.print("Game Over"); } //end main } //end class Y

Use Quizgecko on...
Browser
Browser