L04: Repetition Flashcards

1
Q

What are the 3 types of loops?

A

while, do-while, for

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

Describe a while loop.

A

While a condition is true, the statement is executed repeatedly.

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

What happens when a condition is always true in a while loop?

A

Infinite loop.

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

Describe a do-while loop.

A

The statement is executed before the condition is evaluated.

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

What’s the main difference between while and do-while?

A

The do-while loop executes the statement at least one time.

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

Describe the 4 elements in a for loop.

A

initialization A. condition B. statement C. update D

for (A; B; D)
C;

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

Are expressions in a for loop optional?

A

Yes, any part can be omitted, however the semi-colons must remain.
ex. for (int j=0; ; j++)

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

When should we use while vs for.

A

while is used when we don’t know how many times we want to execute a statement. for is used when we know how many times we want to execute a loop body.

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

for (i=0, sum=0; i<10; sum+=i,i++);
Is this valid syntax?

A

Yes, the for loop is executed however, no statement is executed.

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

If we want multiple expressions in a for loop what can we use?

A

Commas separating each expression can be used.
ex. for(i=0, j-1; i<=10; i++, j*=2) –> VALID

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