Presentation is loading. Please wait.

Presentation is loading. Please wait.

Incrementing ITP © Ron Poet Lecture 8.

Similar presentations


Presentation on theme: "Incrementing ITP © Ron Poet Lecture 8."— Presentation transcript:

1 Incrementing ITP © Ron Poet Lecture 8

2 Adding 1 To A Variable Adding 1 to a variable is a very common task.
yearCount = yearCount + 1. It is so common that there is a special notation. yearCount++; The ++ operator means add 1. There is also a -- operator. ITP © Ron Poet Lecture 8

3 Adding Something to a Variable
This is also very common prices = prices + inflation * prices. There is a special notation. prices += inflation * prices += means add the right hand side to the variable. There is also a -= operator. ITP © Ron Poet Lecture 8

4 Many Similar Operators
Most operators have an = version. *=, /=, %= They are convenient and make your program easier to read. ITP © Ron Poet Lecture 8

5 Counting Loops ITP © Ron Poet Lecture 8

6 Counting Loops It is common to want to iterate for a given number of times. Here is a loop that counts from 1 to 10. The variable i takes the values 1, 2, 3, . . ., 10 inside the loop. int i = 1; while (i <= 10) { // do something with i; i++; } ITP © Ron Poet Lecture 8

7 For Loop This pattern is so common that there is a special loop for it. The previous code can be written as. for (int i = 1; i <= 10; i++) // do something with i; It is almost exactly the same as using a while loop. It is easier to read because the loop control is all in one place, at the start of the loop. ITP © Ron Poet Lecture 8

8 The Scope of i The difference is the scope of the loop variable i.
It is restricted to the for loop and cannot be used after the loop has finished. If we want to use i after the loop, we must define it before the loop. int i; for (i = 1; i <= 10; i++) // do something with i; ITP © Ron Poet Lecture 8

9 Only ;; is Essential The for loop has three parts.
Initial action: int i = 0 The test: i <= 10 Action at end of loop: i++ These do not have to be there. Omit them if they are not necessary. for (;;) is legal, an infinite loop! ITP © Ron Poet Lecture 8

10 Types of Loops ITP © Ron Poet Lecture 8

11 Test First Loops Continue Test false true Body of Loop After the Loop
ITP © Ron Poet Lecture 8

12 Exit in Middle Loop First Part of Loop Exit Test true false
Second Part of Loop After the Loop ITP © Ron Poet Lecture 8

13 Which Loop? while and for loops are test first loops.
The first thing that is done is check the test to see if it is true. More complicated loops can do something first. test in the middle of the loop. do something else go round again ITP © Ron Poet Lecture 8

14 Exit in Middle Styles There are two main ways of doing an exit in the middle loop. Both have their advocates and detractors. One is the 'if - break out of middle' style. The other is the 'boolean variable – else' style. ITP © Ron Poet Lecture 8

15 Example Situation Here is a possible loop design for an example situation. Loop Get a response from the user. If user says NO exit process the user's response. ITP © Ron Poet Lecture 8

16 if – break Style Java has a break statement which means exit the loop.
It is normally controlled by an if statement. We only exit the loop if the exit test is true. This is different from the while loop. while test true means go round again. if test true means stop loop. The actual loop statement is an infinite loop. Because we control the exit from the middle. ITP © Ron Poet Lecture 8

17 if – break code for (;;) // infinite loop { con.print("Continue? ");
String response = con.getWord(); if (response.equals("NO")) break; // exit the loop con.println("You typed " + response); } ITP © Ron Poet Lecture 8

18 boolean – else Style We define a boolean variable, usually called done, which we test at the start of the loop. It is initialised to false. We set it to true if the exit test is true. The second part of the loop is in an else. ITP © Ron Poet Lecture 8

19 boolean - else code boolean done = false; while (!done) {
con.print("Continue? "); String response = con.getWord(); if (response.equals("NO")) done = true; // exit the loop later on // so skip the second part of the loop else con.println("You typed " + response); } ITP © Ron Poet Lecture 8

20 Which is Best if – break is closest to the design.
Reading the code tells you immediately that you are in an 'exit in middle' loop. Program proving works better if all loops are tested at the start. Automatic tools work better. ITP © Ron Poet Lecture 8


Download ppt "Incrementing ITP © Ron Poet Lecture 8."

Similar presentations


Ads by Google