Part One: Understanding the basicsWhile loops allow us to create a repetition of code until a specific condition has been met. When we use a while loop there is no guarantee of many times the code will be repeated. While loops are sometimes thought of a repeating if statement. They can be a useful way of ensuring your users enter valid inputs.
You may have already worked with for loops. The main difference is that a for loop has a pre-determined number of loop repetitions. While loops have an indefinite number of iterations, we cannot tell when they will stop! For loops on the other hand have a definite number of iterations, we know how many times they will loop. Task 1: Think about the following situations and decide if a while loop would be suitable:
a) Adding 11 players to a football team sheet b) A countdown timer on a computer game c) Find all prime numbers up to 1000 d) Checking the number of lives remaining in a computer game e) Search through a list of 50 words for total number of vowels |
Part Two: While loop flowchartThe flowchart for a while loop should show that the conditional code is happening within the loop. Take a look at figure 1. The loop will execute the conditional code until a condition is met. The loop then breaks and the programme continues (or not as the case may be!).
Figure 2 shows a simple while loop which will add 1 to a variable called sum until the user enters n. The resulting code is shown below.
Figure 2 executed code: |
figure 1
figure 2
|
figure 3a
figure 3b
figure 4a
figure 4b
|
Part Three: While loop codeThere are two approaches to writing code for a while loop:
1. While loop using a standard variable
Figure 3a: Line 1 simply creates an empty variable called quit. The next line will execute the code within the loop until the user enters y (remember != means not equal to). Line 3 is code is indented within the while loop meaning it will be executed until the user meets the condition. Line 4 shows the message which is output when the loop breaks. Figure 3b: Although our output message offers y/n the programme will accept any other character. The only way to break the loop is for the user to enter y. 2. While loop using a Boolean variable Figure 4a: Using Boolean variables to control a while loop is very common. In the example shown we initiate a variable called quit. This is set to false as default. The while begins by checking that quit is still equal to false. The if selection allows the while loop to check for an input y. Once this happens quit becomes true and an appropriate message is printed. Figure 4b: The screenshot is the same for a reason, they do the same thing! You will find uses for both types of loop syntax so its useful to be aware of both. Task 2: Write 2 of your own simple while loops using the example code in figures 3a and 4a. Save these as while with variable and while with boolean variable. |
Part Four: While loop exercises
Task 3: Attempt all of or some of the while loop tasks below:
a) A while loop that replicates the programme in figure 2 b) A while loop that asks a user to enter a name over 3 characters in length. If the user enters less than 3 the loop iterates, when they enter over 3 the loop breaks. c) A while loop that asks the user if they would like a random number between 1 and 20. The loops continues until the user inputs n. Add to this a variable that totals up all of the random numbers d) A while loop that only allows the input of a number between 10 and 20 before it breaks e) A while loop that asks a user if they would like to start a game, if they input y a message reads "game about to start", if they enter n a message reads "ok, goodbye", any other input should display "sorry, enter y/n only" f) A while loop that checks if a password has been entered correctly. A message should read "password denied" until the user inputs the correct password "compscirules" |
|
Part 5: Games!Task 4: To really push yourself here try to create the following games:
a) A higher or lower guessing game. The player is given a random number (maybe between 1 and 10) and then given a choice. They can guess whether the next number will be higher or lower. The programme then generates a random number and compares this to the player's number. If they guessed right their score increases and the game continues, if not...game over. b) Find the hidden number. The programme generates a random number between 1 and 100. The player then guesses the number, as they guess they are given guidance as to whether the hidden number is higher or lower than the guess. You could limit the number of guesses to make the game more interesting. c) Slot machine. The player is given 10 credits. The programme generates 3 random numbers and with each 'spin' deducts a credit. If the random numbers match (3,3,3 or 3,3,4 etc) then award extra credits. d) Rummy. A player is given three random numbers. The aim of the game is to either match all 3 numbers currently held (e.g. 222 or 555) or to get a run (e.g. 123 or 456). Each round they choose which card (number) they wish to swap. This is replaced by a random number. The game ends when they achieve a run or treble or once they run out of credits. |