ItP 2 - While Loops Flashcards
for loops iterate over a sequence of values, such as a list, while ‘while loops’
continue as long as a specified condition is true.
t’s easy to accidentally create an infinite loop with a while loop if the
loop condition never becomes false, thus causing the loop to continue indefinitely.
In many programming environments, including Python and the Linux terminal, pressing Ctrl-C interrupts the execution of a program and In Colab you can hit the ‘stop’ (square) button that appears when you run a cell. On Spyder (we will deal with Spyder shortly), press the red square, stop button in the console pane., allowing you to - (2)
break out of an infinite loop or stop a long-running process.
It’s a handy trick for regaining control when you get stuck on infinite loops
Unlike for loops, While loops are useful and often used when the number of loop iterations is not
known in advance and loop continues until a certain condition becomes false
For loops, the decision of how many times to iterate is made at
at the start of the loop.
In while loops, we perform a test at each time we go around the loop and
decides to continue
Write
variable ‘a’ is equal to 0
Produce a while loop that while a is less than 5 then adds 1 to a and prints it
Once condition of while loop is false, it prints Done
Explain this while loop - (7)
Our loop tests whether or not a is less than 5.
If it is, the loop continues.
We print the value of a then we increase it by 1.
Then we go back to the test again. The loop keeps iterating until condition ‘a < 5’ becomes false’
Once ‘a’ reaches 5 condition is no longer met and loop terminates
After loop it prints Done indicating end of program
Note the += syntax here: this is the same as writing a = a + 1.
What is output of this while loop code?
Create a list containing the words apple, banana, cherry and damson. Write a while loop which iterates over the list and prints out the words one at a time. You will need to have the while loop count over the indices and exit when you get to the end of the list.
Explain this code - (6)
fruitList: This is a list containing the words ‘apple’, ‘banana’, ‘cherry’, and ‘damson’.
fruitCounter: This variable is initialized to 0. It will be used to keep track of the current position in the fruitList
The while loop is set up with the condition fruitCounter < len(fruitList). This means that the loop will continue executing as long as the fruitCounter is less than the length of the fruitList. In other words, it will iterate over the list until the end is reached.
Inside the loop, print(fruitList[fruitCounter]) is used to print the element of fruitList at the index indicated by fruitCounter
fruitCounter += 1 is used to increment the value of fruitCounter by 1 in each iteration. This ensures that the loop moves to the next element in the fruitList on each iteration.
The loop continues to execute until fruitCounter becomes equal to or exceeds the length of fruitList, at which point the condition fruitCounter < len(fruitList) becomes false, and the loop terminates.
As a result of the loop, each word in fruitList is printed out one at a time, in the order they appear in the list.