1 / 9

Termination of a Loop

Termination of a Loop. Contents. Terminating a loop by count Terminating a loop by a sentinel Terminating a loop reading an unspecified number of tokens Terminating a loop by a flag Terminating a loop using the break statement The continue statement. Termination of a loop. By count.

mikko
Download Presentation

Termination of a Loop

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. Termination of a Loop Contents • Terminating a loop by count • Terminating a loop by a sentinel • Terminating a loop reading an unspecified number of tokens • Terminating a loop by a flag • Terminating a loop using the break statement • The continue statement

  2. Termination of a loop By count Typical situation: the number of iterations has been determined in advance. final int maxCount = 25; for (int count = 0; count < maxCount; count++) { //do something }

  3. Terminate loop when sentinel value occurs Termination of a loop By a sentinel value Example Use a value that is not in the range of data values. String str; str = JOptionPane.showInputDialog(“Enter exam grade–end with –1”); int term = Integer.parseInt(str); int i = 0; while(term != -1) { A[i] = term; i++; str = JOptionPane.showInputDialog(“Enter exam grade–end with –1”); term = Integer.parseInt(str); } The user is prompted for a value before entering the loop and during every iteration of the loop. Will a do-while loop work instead?

  4. Value is stored in the array before it is tested Terminate loop when sentinel value occurs Termination of a loop By a sentinel value Example Assume there is at least one grade and try a do-while loop String str; int i = 0; do { str = JOptionPane.showInputDialog(“Enter exam grade–end with –1”); term = Integer.parseInt(str); A[i] = term; i++; }while(term != -1); Danger! Even if we assume there is at least one legitimate grade, the sentinel value still gets stored in the array of grades. Sentinels introduce a dummy value that is not in the domain of the data set and is not to be processed like the other data. An error is introduced if the user forgets to include a sentinel in the data or the if a sentinel is processed as data.

  5. Use StringTokenizer method to test for more tokens Retrieves next token from inStr and moves the “cursor” past the current token. Termination of a Loop An undetermined number of tokens Java has a better way of testing for the end of an input string than using a sentinel. Since one does not read integers directly from the keyboard or from a text file, terminating loops with a sentinel is not frequently used in Java. String inStr = JOptionPane.showInputMessage(“enter some integers”); StringTokenizer st = new StringTokenizer(inStr); int sum = 0; while (st.hasMoreTokens( )) { String s = st.nextToken( ); int term = Integer.parseInt(s); sum += term; }

  6. Termination of a loop By setting a flag Typical situation: Testing whether a key value is present in an array boolean flag = false; int index = 0; while (!flag && index < theArray.length) { flag = (A[index] == key); index++ }

  7. Conditional and -- If the first term is false, the second is not tested. (Prevents trying to test at an out of range index) Must test whether index is in range first Terminating a loop The loop in the previous example can be written without using an explicit boolean variable. int index = 0; while (index <A.length && A[index] != key) index++; //loop has terminated when we reach here – determine if key is found if (index < A.length) System.out.println(“key is found at index “ + index); else System.out.println(“key does not occur in the array”); //loop terminated before reaching end of array – key found at index //loop terminated by passing the end of the array – key not present

  8. Terminating a loop Loop forever and terminate with a break for ( ; ; ) { //do something if (boolean-condition) break; //zero or more additional statements to be executed if condition is false } //after the break control moves to here – the next statement after the loop //no variant is initialized and incremented, no test for termination is done – program will loop forever without a break

  9. When no values remain to be read null returned If readLine( ) encounters an empty line in the file, a null string is returned Termination of a loop The continue statement Consider the following example String inStr = “”; double value; try { BufferedReader in = new BufferedReader(new FileReader(“input.txt”)); for( ; ; ) { inStr = in.readLine( ); if (inStr == null) break; if (inStr.equals(“”) ) continue; //do something more } After continue, return to the top of the loop and read new input When break is executed, control leaves the loop and jumps to here

More Related