Anda di halaman 1dari 2

Notes on JAVA 10.2005.

4: Loops

Page 1 of 1

Notes on JAVA 10.2005.4: Revision of Loops:


A. There are THREE important principles to remember when you use while- and do-loops: You can remember these three principles with the letters I-T-C: 1. The I stands for INITIALISE. This means that the variable(s) that is/are used to control the loop should be declared and have (a) start (initial) value(s) before the loop is started. 2. The T stands for TEST. A condition must be tested at the beginning (while-loop) or end (do-loop) of the loop. If the condition is (still) TRUE, the body of the loop will be executed. 3. The C stands for CHANGE. The variables used in the condition (that will ultimately stop the loop) MUST change inside the loop to ensure that the condition will eventually become FALSE. The following can be done: Any mathematical calculation performed that changes the variable (usually a 1 ( or any other number) is added or subtracted to or from the variable but other operations are allowed). A new value for the variable can be read in (from keyboard or file). NB: In the for-loop this happens automatically: I T C for (int i = 0; i < 10; i++) i.e. i has the initial value 0; the loop tests that i is still smaller than 10 before it will execute the loop again, i is incremented by 1 on each execution of the loop (i++ or i = i + 1). B. If the condition is FALSE from the start, the statement(s) of the loop are never executed (while loop). However, a do-loop executes at least once. You may use more than one statement in the loop. They must then be blocked between { and } brackets. Other loop and/or decision structures can be nested within the loop. If your loop-controlling variable(s) never change(s), you have created an infinite loop. When designing loops, ask yourself the following questions: 1. Is there repetition? If YES, then use a loop, otherwise not. 2. Use the suitable (correct) type of loop: The for-loop for fixed number of repetitions. The while-loop is the pre-test loop and might not execute at all. The do-while-loop is the post-test loop and will always execute at least once. 3. Before you can initialise, you must ask: What is the test condition of the loop? What, therefore, must be initialised? How is the variable of the condition changed? Mathematically or by reading new values? 4. Where is the output? Is it before, inside or after the loop? 5. Is there selection (i.e. decision structures)? If YES, then use IF structures.

C.

D. E.

F: Four steps to successful programming: Step 1: Read and understand the problem (i.e. emphasis on problem-solving). Read the problem carefully. Make sure you understand what is asked. Step 2: Analysis of input and output. Ask yourself what data is needed by the program and what information is needed from the program. Step 3: Break down the problem into steps and determine their sequence. Ask yourself the standard questions above. Step 4: Then design your algorithm and ultimately write your program code.

Notes on JAVA 10.2005.4: Loops

Page 2 of 2

Exercises:
1. Write a program that reads a sequence of real numbers and computes the maximum, minimum, and average of these. You could first ask the user for the number of values in the sequence and then use a for-loop to prompt for and read the numbers in the sequence, OR use a sentinel value to indicate that you want to stop reading in more numbers, OR ask a question (e.g. More?) each time. NB: Guard against division by zero when computing the average. Write a program DisplayOddPositives that can display a given number of positive odd integers as determined by input. To do this successfully you will have to devise a means to increment the counter by 2! Write a program that displays a table of the sums 1 + 2 + 3 + --- + n for every value of n from 1 to 15. Design an algorithm that allows a person to guess a number between 1 and 100. With each guess, an indication must be given as to whether the number was too high or too low. The user must then be given another chance to guess until the correct number is guessed. Print a message of congratulations when the correct number has been guessed, stating the number of guesses needed. Then exit the program (or more advanced ask if the user wants to guess another number (do-loop)). Use a while-loop structure. To get the computer to give you a random number (which to user has to guess), use the following code: double x = Math.random (); x = x * 100; S.o.println (x); int randomNumber = (int) x; S.o.println (randomNumber); Generates a random number in double format. or x *= 100;

2.

3. 4.

Converts x (a double) to integer format.

Try this, and later remove the two S.o.println statements. The fact that x has been multiplied by 100 makes it a random number between 0 and 100. If bigger formats are required, other multiples of 10 can be used. A suitable type must be declared!

Good luck, enjoy it!

_________________________________________________

mb

Anda mungkin juga menyukai