Loops Flashcards

1
Q

What are the 2 advantages of Looping?

Understanding the Advantages of Looping

A
  • Looping makes computer programming efficient and
    worthwhile
  • Write one set of instructions to operate on multiple,
    separate sets of data

Understanding the Advantages of Looping

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

What is a Loop in programming?

Understanding the Advantages of Looping

A

Loop: structure that repeats actions while some
condition continues

Understanding the Advantages of Looping

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

TRUE OR FALSE: As long as a Boolean expression remains true, a while loop’s body executes. It also controls the number of repetitions

Understanding the Advantages of Looping

A

TRUE

Understanding the Advantages of Looping

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

What type of variable fits this function?
* initialized before entering loop
* Loop control variable tested
* Body of loop must alter value of loop control variable

Using a Loop Control Variable

A

Loop control variable

Using a Loop Control Variable

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

What are the two ways that controls the repetition of a loop variable?

Using a Loop Control Variable

A

Repetitions are controlled by:
* Counter
* Sentinel value

Using a Loop Control Variable

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

What type of loop executes the predetermined number of times in a loop?

A

Definite loop

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

What type of loop program counts loop repetitions

Using a Definite Loop with a Counter

A

Counter-controlled loop

Using a Definite Loop with a Counter

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

Loop control variables altered by?

Using a Definite Loop with a Counter

A

Incrementing - adding
Decrementing - reducing

Using a Definite Loop with a Counter

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

What type of loop performs a different number of times each time the
program executes

A

Indefinite Loop

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

What are the 3 crucial steps in using an indefinite loop?

Using a Indefinite Loop with a Sentinel Value

A

Three crucial steps:
* Starting value to control the loop must be provided
* Comparison must be made using the value that controls
the loop
* Within the loop, value that controls the loop must be
altered

Using a Indefinite Loop with a Sentinel Value

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

What are the three steps that should occur in every properly functioning
loop?

Understanding the Loop in a Program’s
Mainline Logic

A

Three steps that should occur in every properly functioning
loop:
* Provide a starting value for the variable that will control
the loop
* Test the loop control variable to determine whether the
loop body executes
* Alter the loop control variable

Understanding the Loop in a Program’s
Mainline Logic

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

What is it called when there are loops between loops?

Nested Loops

A

Nested Loops

Nested Loops

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

What type of loop contains the other loop?

Nested Loops

A

Outer Loops

Nested Loops

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

What type of loop is contained?

Nested Loops

A

Inner Loops

Nested Loops

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

What type of loop is needed when values of two (or more) variables repeat to produce every combination of values?

Nested Loops

A

Nested Loops

Nested Loops

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

What are the 4 common loop mistakes?

Avoiding Common Loop Mistakes

A
  • Neglecting to initialize the loop control variable
  • Neglecting to alter the loop control variable
  • Using the wrong comparison with the loop control variable
  • Including statements inside the loop that belong outside the
    loop

Avoiding Common Loop Mistakes

17
Q

What are the 4 common loop mistakes?

Avoiding Common Loop Mistakes

A
  • Neglecting to initialize the loop control variable
  • Neglecting to alter the loop control variable
  • Using the wrong comparison with the loop control variable
  • Including statements inside the loop that belong outside the
    loop

Avoiding Common Loop Mistakes

18
Q

What common loop mistake is this?

Example: get name statement removed
* Value of name unknown or garbage
“hello 12BGr5”
* The uninitialized value of name is “ZZZ” the program eds
immediately before the user can enter any namesWhat are the 4 common loop mistakes?

Avoiding Common Loop Mistakes

A

Neglecting to initialize the loop control variable

Avoiding Common Loop Mistakes

19
Q

What common loop mistake is this?

  • Remove get name instruction from outer loop
  • User never enters a name after the first input one
  • Inner loop executes infinitely
  • Always incorrect to create a loop that cannot terminate

Avoiding Common Loop Mistakes

A

Neglecting to alter the loop control variable

Avoiding Common Loop Mistakes

20
Q

What common loop mistake is this?

  • Remove get name instruction from outer loop
  • User never enters a name after the first input one
  • Inner loop executes infinitely
  • Always incorrect to create a loop that cannot terminate

Avoiding Common Loop Mistakes

A

Neglecting to alter the loop control variable

Avoiding Common Loop Mistakes

21
Q

What common loop mistake is this?

Using < instead of <= or using > instead of >=
Seriousness depends on actions performed within a loop
Overcharge insurance customer by one month
Overbook a flight on airline application
Dispense extra medication to patients in pharmacy

Avoiding Common Loop Mistakes

A

Using the wrong comparison with the loop control
variable

Avoiding Common Loop Mistakes

22
Q

What common loop mistake is this?

Example: discount every item by 30 percent
Inefficient because the same value is calculated 100
separate times for each price that is entered
Move outside loop for efficiency

Avoiding Common Loop Mistakes

A

Including statements inside the loop that belong
outside the loop

Avoiding Common Loop Mistakes

23
Q

True or False?

The for statement or for loop is a definite loop.

Using a for Loop

A

True

Using a for Loop

24
Q

What type of loop provides these 3 actions in 1 structure?
* Initializes
* Evaluates
* Increments

Using a for Loop

A

For Loop

Using a for Loop

25
Q

What type of loop takes this form?

for loopControlVariable = initialValue to finalValue step stepValue
        do something
endfor

Using a for Loop

A

For loop

Using a for Loop

26
Q

What type of loop takes this form?

for count = 0 to 3 step 1
output “Hello”
endfor
Initializes count to 0
Checks count against the limit value 3
If evaluation is true, for statement body prints the label
Increases count by 1
  

Using a for Loop

A

For loop

Using a for Loop

27
Q

True or False?

while statement could be used in place of for statement

Using a for Loop

A

True

Using a for Loop

28
Q

True or False?

while statement could be used in place of for statement

Using a for Loop

A

True

Using a for Loop

29
Q

What is this?

This is a value, a number used to increase a loop control variable on each pass
through a loop

Using a for Loop

A

Step value

Using a for Loop

30
Q

True or False?

Programming languages can:
Require a statement that indicates the step value
Have a step value default of 1
Specify step value when each pass through the loop changes the loop control
variable by value other than 1

Using a for Loop

A

True

Using a for Loop

31
Q

True or False?

Programming languages can:
* Require a statement that indicates the step value
* Have a step value default of 1
* Specify step value when each pass through the loop changes the loop control
variable by value other than 1

Common Loop Applications

A

True

Common Loop Applications

32
Q

What loop application is this?

What is the variable that gathers:
* values similar to a counter
* Counter increments by one
* Accumulator increments by some value

Common Loop Applications

A

Accumulator

Common Loop Applications

33
Q

What loop application is this?

When using a loop to validate data
When prompting a user for data, there is no guarantee that data is valid

Common Loop Applications

A

True

Common Loop Applications

34
Q

What loop application is this?

What type of application makes sure that data falls in acceptable ranges?
Example: user enters birth month
If number is less than 1 or greater than 12
Display error message and stop the program
Assign default value for the month
Reprompt the user for valid input

Common Loop Applications

A

Validate data

Common Loop Applications

35
Q

What loop application is this?

____________ requires a variety of methods
isNumeric() or similar method
* Provided with the language translator you use to write your
programs
* Black box
isChar() or isWhitespace()
Accept user data as strings
Use built-in methods to convert to correct data types

Common Loop Applications

A

Validating a data type

Common Loop Applications

36
Q

What loop application is this?

  • Many data items can be checked for reasonableness
  • Good defensive programs try to foresee all possible
    inconsistencies and error

Common Loop Applications

A

Validating reasonableness and consistency of data

Common Loop Applications