Unit 4 - Iteration Flashcards Preview

AP Computer Science A > Unit 4 - Iteration > Flashcards

Flashcards in Unit 4 - Iteration Deck (23)
Loading flashcards...
1
Q

iteration statements

A

Change the flow of control by repeating a set of statements zero or more times until a condition is met

2
Q

Loop

A

Repetition of statements

Repeating a series of steps over & over and over

3
Q

Structure of while loop

A
while (/*Boolean expression/*)
{
      //statement one;
      //statement two;
      // ...
}
4
Q

Loop control variable

A
//initialize LCV
while (/* check LCV*/)
{
      //statement one;
      //statement two;
      //....
      //update loop control variable
}
5
Q

A loop is an infinite loop when

A

the Boolean expression always evaluate to true

6
Q

If the Boolean expression evaluates to false initially,

A

the loop body is never executed at all

7
Q

Algorithm to sum individual digits given a number

A

Using mod, integer division, and while loop to do so

1 - Given an integer called number
2 - Create a variable for the sum
3 - Use mod to isolate the last digit of the number
4 - add the last digit to sum
5 - use integer division to eliminate the last digit from the number
6 - Repeat while there are still digits
7 - display the sum

8
Q

Algorithm to calculate the number of years it would take to reach your goal using Interest

A

1 - Given an integer called numYears
2 - Create a variable for the total
3 - Add total to total multiplied by 10 years
4 - increase year by one and print out yearly balance
5 - Repeat until total reaches 1000
6 - Display numYears

9
Q

Algorithm to sum first 10 positive multiples of 4

A
1 - Given an integer called counter
2 - Create a variable for the sum
3 - Add sum to counter multiplied by 4
4- Increase counter by one
5 - Repeat until value reaches 10
6 - Display sum
10
Q

for loop header

A

3 parts
initialization
Boolean expression
increment or decrement

11
Q

in a for loop, initialization is

A

only executed once before the first Boolean expression evaluation

12
Q

In each iteration of a for loop, the increment statement is executed after

A

the entire loop body is executed and before the Boolean expression is evaluated again

13
Q

Structure of a for loop

A

for (initialization;Boolean expression; update)’

14
Q

a for loop can be rewritten into an

A

equivalent while loop and vice versa

15
Q

When statements need to repeat a defined number of time, which type is more appropriate?

A

for loop may be more appropriate as start and end values can be seen on one line

16
Q

When repeating statements an x amount of times or based on complex Boolean expression, which type is more appropriate?

A

while loop as while statement isolates the Boolean expression

17
Q

When updating to LCV is more complex than a simple increment or decrement, which type is more appropriate?

A

while loop as you can update LCV over a series of statements to make it more readable

18
Q

OBOE

A

iteration statement loops one time too many or too few

19
Q

Common mistakes made in initialization

A

Should a variable start at 0 or 1

20
Q

Common mistakes made in boolean expression

A

Should we use < or <=

21
Q

Common mistakes made in updating

A

Should our value increase or decrease by 1 each time

22
Q

How to prevent mistakes

A

Test loop with small values

THINK

23
Q

Algorithm for finding substring within a String

A

ggg