CS115 lecture notes, Spring 1999 Week 6, Friday (some of this material is a repeat from Wednesday, since I didn't actually get to deliver it) Review ------ Two kinds of methods in the world: void and non-void Void methods print something or draw something, but produce no return value. Non-void methods produce a return value and _usually_ print nothing (although they can). Often while you are debugging non-void methods, you will have print statements, but then you remove them at the end (or comment them out). This kind of code, which is used to help write other code, is called scaffolding. Multiple assignment ------------------- int fred = 5; fred = 7; 1) Old value replaces new. A variable always contains only one value. 2) You can assign a value as many times as you want, but you can only declare once. int fred = 5; int fred = 7; // ERROR 3) You can change the value of parameters by assigning values to them, but it is almost never a good idea. Of course, changing the value of a parameter has no effect on the caller (who provided the original values, but has no interest in the final values). Iteration --------- Doing things repeatedly in programs is so common that there is a program construct for it. It is not necessary, since we already know how to iterate using recursion, but it is sometimes easier to write iterative programs. The while statement ------------------- 1) the syntax is identical to a simple if statement while (condition) { body } 2) The condition has to be a boolean expression 3) The body can contain any kind of statement, including declarations, assignments, method invocations, if statements, and even while statements 4) The semantics are almost like the English: While the condition is true, keep repeating the body over and over until the condition becomes false. 5) Iterative structures are called loops. Each time the loop is executed is called an iteration. Example ------- public static void countup (int n) { int i = 0; while (i <= n) { System.out.println (i); i = i+1; } System.out.println ("Blastoff!"); } In this case i is called a loop variable, because it is the variable that keeps track of the iterations and determines when the loop terminates. i = i + something; Is a common idiom in which the new value is based in part on the old value. Don't forget that the old value had better be initialized, since we are going to read it before we write it. Traversal --------- A simple variation on countup is a loop that goes from a lower bound to an upper bound: public static void traverse (int m, int n) { int i = m; while (i <= n) { System.out.println (i); i = i+1; } } Now all we need to add is an accumulator to add up all the numbers between m and n: public static int itSum (int m, int n) { int i = m; int total = 0; while (i <= n) { total = total + i; i = i+1; } return total; } You might want to read Section 7.7 Internal variables ------------------ If you declare a variable inside an if or while statement, it only exists within that statement! public static int itSum (int m, int n) { int i = m; while (i <= n) { System.out.println (i); int total = total + i; i = i+1; } return total; // ERROR: total does not exist outside the loop } Infinite loops -------------- Somehow or other, the body of the loop should do something that eventually makes the condition false. If the condition is always true, the loop will run forever, which is usually not what you want. This requirement is similar to what I said about recursive methods, which is that you have to make sure you reach the base case eventually. In some cases, we can't tell whether the method will terminate. public static void sequence (int n) { while (n != 1) { System.out.println (n); if (n%2 == 0) { // n is even n = n / 2; } else { // n is odd n = n*3 + 1; } } } But most of the time you can. QUIZ MONDAY!