Presentation is loading. Please wait.

Presentation is loading. Please wait.

While Revisited u The while construct is (probably) the most frequently used iteration construct. u From the examples of it’s use we can see that it has.

Similar presentations


Presentation on theme: "While Revisited u The while construct is (probably) the most frequently used iteration construct. u From the examples of it’s use we can see that it has."— Presentation transcript:

1 while Revisited u The while construct is (probably) the most frequently used iteration construct. u From the examples of it’s use we can see that it has essentially three logical components u an initialisation to ensure the start state is correct (normally occurs prior to the loop commencing) u a condition to test the current state so as to determine whether the loop should continue u a mechanism (inside the loop) which is capable of changing the state so that the condition can become false (i.e. the loop stops/terminates) u This pattern is characteristic of ALL while loops.

2 while Revisted u When reading the text of a program one must be reasonably diligent to spot the ‘textual’ location of the various components of a while construct. u This is particularly true of situations where the loop body contains a lot of code or where the code is relatively complex. u Java, and other languages, provides an alternative iteration construct called the for which has the same functionality as the while but is more compact. u All of the loop control infrastructure is gathered into a single location.

3 Syntax of for Construct for (initial state statement(s) ; continuation test or repeated state inspection ; state change i.e. after executing loop body) { loop body statement(s) ; } u The keyword for signals the beginning of an iteration construct. u Note, the three logical components of a while are contained within the for brackets ().

4 Syntax of for Construct u The three components are separated using the semi-colon (;) character. u The initialisation component is executed once only, BEFORE the loop mechanism begins. u Like the while construct, the state inspection or continuation condition is tested on each iteration BEFORE the loop is executed. u The state change component is executed after ALL of the loop body statements have been executed.

5 while example - typeText public String typeText(String delimiter) { String key, text ; clearDisplay() ; text = ""; key = waitForKeyPress(); while(key.compareToIgnoreCase(delimiter) != 0) { text = text + key ; setDisplay(text) ; key = waitForKeyPress(); } return text ; } Set initial state Repeated state test (i.e. continue?) Change state

6 for equivalent - typeText public String typeText(String delimiter) { String key, text ; clearDisplay() ; text = ""; for ( key=waitForKeyPress() ; key.compareToIgnoreCase(delimiter) != 0 ; key=waitForKeyPress() ) { text = text + key ; setDisplay(text) ; } return text ; } Initial state Repeated state test (i.e. continue?) change state

7 while example - find public int find(String str) { int i ; i = 0 ; while(i < stringCount && str.compareToIgnoreCase(list[i]) != 0 ) { i++ ; } if(i < stringCount) { return i ; } else { return -1 ; } Set initial stateRepeated state test (i.e. continue?) Change state ALSO loop body!

8 for equivalent - find public int find(String str) { int i ; for (i=0 ; i < stringCount && str.compareToIgnoreCase(list[i]) != 0 ; i++ ) ; if(i < stringCount) { return i ; } else { return -1 ; } NOTE: if there is NO LOOP BODY just use a semi-colon Initial state Repeated state test (i.e. continue?) change state

9 while Example - remove public void remove(String str) { int i, strIndex ; strIndex = find(str) ; if(strIndex != -1) { i = strIndex + 1 ; while(i < stringCount) { list[i-1] = list[i] ; i++ ; } stringCount-- ; } Set initial state Repeated state test (i.e. continue?) Change state

10 for equivalent - remove public void remove(String str) { int i, strIndex ; strIndex = find(str) ; if(strIndex != -1) { for (i = strIndex + 1 ; i < stringCount ; i++ ) { list[i-1] = list[i] ; } stringCount-- ; } Initial state Repeated state test (i.e. continue?) change state

11 while example - display public void displayFirstToLast() { int i ; i = 0 ; while(i < stringCount) { System.out.println(list[i]) ; i++ ; } Set initial state Repeated state test (i.e. continue?) Change state

12 for equivalent - display public void displayFirstToLast() { int i ; for (i = 0 ; i < stringCount ; i++ ) { System.out.println(list[i]) ; } Set initial state Repeated state test (i.e. continue?) Change state

13 for equivalent - display public void displayLastToFirst() { int i ; for (i = list.length ; i >= 0 ; i-- ) { System.out.println(list[i]) ; } Set initial state Repeated state test (i.e. continue?) Change state

14 Enhanced for/for-each u Java now includes a variant to the basic for loop called the enhanced for or for-each (mainly because other programming languages have called it that). u The enhanced for makes iteration over and entire array or other type of collection much more convenient.

15 Enhanced for/for-each u The enhanced for loop can make code much clearer but it has a number of limitations u You can only inspect array elements – you can’t alter them. u You can only manipulate a single element (i.e. you can’t manipulate two or more elements in the loop body) u You can only traverse the list in a forward direction (i.e. from first to last) in single steps u You have Java 5 or later.

16 Enhanced for/for-each syntax for( elementtype var : arrayname) { loop body // e.g. System.out.println(var) ; }

17 Enhanced for/for-each example public void displayFirstToLast() { for(String s : list) { System.out.println(s) ; }


Download ppt "While Revisited u The while construct is (probably) the most frequently used iteration construct. u From the examples of it’s use we can see that it has."

Similar presentations


Ads by Google