ItP 2 - While Loops Flashcards

1
Q

for loops iterate over a sequence of values, such as a list, while ‘while loops’

A

continue as long as a specified condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

t’s easy to accidentally create an infinite loop with a while loop if the

A

loop condition never becomes false, thus causing the loop to continue indefinitely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Unlike for loops, While loops are useful and often used when the number of loop iterations is not

A

known in advance and loop continues until a certain condition becomes false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For loops, the decision of how many times to iterate is made at

A

at the start of the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In while loops, we perform a test at each time we go around the loop and

A

decides to continue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain this while loop - (7)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is output of this while loop code?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain this code - (6)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly