Module 6 - While-Loop Flashcards

1
Q

What are the two types of loops?

A

Condition-controlled and count-controlled

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

Often we have to write code that performs

the same task multiple times. What are the disadvantages of duplicating code?

A
  1. It makes the program large
  2. Time-consuming
  3. May need to be correct in many places
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the ‘while’ loop?

A

While condition is true, do task

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

What are the two parts of the while loop?

A

Test condition for true or false
If true, do the task and test again.
If false, exit structure.

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

What is the syntax for the while loop?

A

while condition:

____statements

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

What does a while loop look like in a flowchart?

A

Condition is in a diamond, if true, statements and go back to the start. If false, exit.

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

What is an iteration?

A

One execution of the body of the loop

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

Why is a ‘while’ loop considered a pretest loop?

A
  1. It won’t execute if condition is false to begin with

2. Requires some steps before the loop

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

What is an infinite loop?

A

While Loops have something in the code that eventually makes the condition FALSE, thereby stopping the loop.
Infinite loops don’t stop until program is interrupted
It occurs when programmer FORGETS to add that code

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

How do you avoid an infinite loop?

A

Make sure to change the while condition variable in the loop code

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

What are the two elements of a running total?

A
  1. A loop that reads each number in a series

2. An accumulator variable

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

What are 4 loop best practices?

A
  1. For variables that are changed inside the loop, initialize them BEFORE the loop
  2. Ensure while condition starts at TRUE
  3. Ensure while condition uses a variable that is CHANGED inside the loop
  4. Don’t place code in the loop unless it’s needed (e.g., printing the final result)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the augmented assignment operators (aka shorthand operators)?

A
\+= 
-=
*=
/=
%= 

for example, x = x+5 would be x += 5

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

What is a sentinel?

A

Special value that marks the end of a sequence of items

When the program reaches this value, it knows the end of the sequence was reached and the loop terminates

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

What are examples of sentinels?

A

Empty line, -1, empty string for input values, etc.

Sentinel value must be DISTINCT from other values so that you can’t confuse with it an actual value in the sequence

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

T or F: Computer can tell the difference between good data and bad data

A

False. It can’t tell.

If user provides bad input, program will produce bad output. (Garbage in, garbage out)

17
Q

It is important to design programs such that bad input is never accepted. What’s one way to accomplish this?

A

Input validation - inspecting input before it is processed by the program.
If input is invalid, prompt user to enter correct data
Can use ‘While loop’ to repeat as long as input is bad

18
Q

What is an update assignment?

A

An assignment where the new value of the variable depends on the old.

19
Q

What is initialization?

A

An assignment that gives an initial value to a variable that will be updated.
It’s like introducing a variable

20
Q

What is ‘break’?

A
Way to break the while loop w/ an if statement
while condition:
\_\_\_\_statements
\_\_\_\_if condition:
\_\_\_\_\_\_\_\_break
\_\_\_\_statements (part of while loop)
21
Q

What are the benefits of using a ‘break’ statement?

A

You can express the stop condition affirmatively (“stop when this happens”) rather than negatively (“keep going until that happens”)

22
Q

What is an algorithm?

A

A mechanical process for solving a category of problems

No intelligence required, process w/ steps according to a simple set of rules

23
Q

What does ‘eval’ do?

A

Takes a string and evaluates it using python interpreter
eval(“1+2*4”)
9
*MUST BE STRING