Chapter 5 Flashcards

1
Q

Why is repetition needed?

A

Allows the use efficient use of variables. It can also input, add and subtract.

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

What is a while expression?

A

Statement that can be simple or compounded

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

What is compounded in terms of a loop?

A

Multiple statements surrounded by {}

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

What is the expression in a loop used for?

A

It acts as a decision maker and is usually a logical expression.

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

What is a counter control loop?

A

When you want to do something a certain amount of times.
int counter=0;
while(counter

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

What are some common mistakes for counter control loop?

A

Make sure that in the loop body that will eventually make the test condition.
Don’t put a semi-colon after the test condition.

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

What is Sentinel-controlled loop?

A
A sentinel or dummy value is tested for to determine if the loop should end.
int sum=0;
cout > num;
while(num != -1)
{
num += num;
cin >> num;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Flag-Controlled Loop?

A
if you are looking for something and can quit once you find it you can set the boolean value to false, once you found what you needed.
bool done = false;
while(!done)
{
Do something...;
if(found what you wanted to)
{
done = true;
}
Do something else...;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

EOF-controlled loop?

A

If you are importing from a file, there is a function called eof() that returns true if you have reached
the end of the file, and false otherwise. Alternatively, the input stream variable itself will return true if
(1) you are not at end of the file and (2) you do not have an input error.
ifstream inFile; // declare the input stream variable
string word; // variable to hold input
inFile.open(“test.txt”); // open the file
while(inFile) // test the loop control variable
{
inFile&raquo_space; word; // update the loop control variable

}

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

What is a do while loop?

A
Work just like a while loop, but are the body of the loop will be run at least once.
Syntax:
do
{
These things
} while(guard statement);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a for loop?

A

This type of loop runs for a specific number of times.
Syntax:
for (initial statement; loop condition; update statement)
{
statement;
}
• The initial statement, loop condition, and update statement are called for loop control
statements
• It is like a counter controlled while loop, but you initialize, compare and update the counter all
on the first line.

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

If you know how many repetitions that are needed for the loop then use a….

A

For loop.

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

If you do not know how many repetitions that are needed for the loop then use the….

A

Do-while loop.

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

If you do not know how many repetitions that are needed for the loop and it might be zero then use the…

A

While loop.

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

What is the break statement used for?

A

To exit early from a loop and to skip the remainder of the switch structure.

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

What doe the rand() function do?

A

It returns a value of int from 0 and 32767.

17
Q

What header file does the rand() function come from?

A

It comes the header file cstdlib.