6. The While Loop Flashcards

1
Q

while loops

A

WHILE some condition holds, DO something repeatedly

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

while loops syntax

A

while (condition)
action

or

while (condition) {
multiple statements
}

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

conditions and ;

A

no ; after the condition’s ) or after the closing }

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

what does a while loop cause

A

causes the body to be executed zero or more times

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

infinite loops

A

ensure loop body changes loop conditions to avoid infinite loops

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

correct while loops

A

1) initialise condition variables
2) test condition
3) modify condition variables

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

types of while loops

A

counter-controlled while loops
sentinel-controlled loops
flag controlled loops

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

counter-controlled while loops

syntax

A

counter = 0;

while (counter < N) {
....
counter++;
..
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

counter-controlled while loops

examples of uses

A

used for:

  • read 20 numbers + average them
  • printout value of investment for each of 35 years
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

note on counter-controlled while loops

A

we may prefer FOR loops instead that are to be executed a specific number of times

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

sentinel-controlled loops

syntax

A

cin&raquo_space; variable

while (variable != sentinel) {
..
cin >> variable;
..
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

sentinel-controlled loops

examples of uses

A

add sequence of numbers terminated by 999

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

choosing sentinel

A

you can initialise the sentinel or input from user

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

flag controlled loops

syntax

A

found = false

while (!found) {
.
.
 if (expression)
   found = true;
.
.
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

flag controlled loops

examples of uses

A

number guessing game

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

random numbers function example

A

random int between 1 and 100 is generated using:

(rand() + time(0))%100

rand() might produce same no. each time so add an int based on current time (time(0), covert to 1…100 range by getting remainder pf dividing random integer by 100

17
Q

rand() is in this library

A

cstlib library

18
Q

time() is in this library

A

ctime library

19
Q

another use for while

A

if you want user to be able to repeat the program