Ch. 5 C++
What header file do you need to include in a program that performs file operations?
#include <fstream>
How many lines will be displayed? int count = 10; while (count < 5) { cout << "My favorite day is Sunday \n"; count = count + 1; }
0
What will this code display? int count = 1; while (count < 10) { cout << count << " "; count = count + 2;
1 3 5 7 9
How many lines will be displayed? int count = 1; while (count < 5) { cout << "My favorite day is Sunday \n"; count = count + 1; }
4
A(n) is a variable that is initialized to some starting value, usually zero, and then has numbers added to it in each iteration of a loop.
Accumulator
What should a program do when it is finished using a file?
Close the file
True/False: A while loop is somewhat limited, because the counter can only count up, not down.
False
When a loop is nested inside another loop, the outer loop goes through all its iterations for each iteration of the inner loop.
False it's the opposite
The loop is ideal for situations that require a counter.
For loop
What will excluding the { } of a while loop do?
Infinite loop!
What is a file's read position? Where is the read position when a file is first opened for reading?
It marks the location of the next file to be read. When first opened, the read position is set to the first byte in the file
What happens if you open an output file and the file already exists?
It will be erased and a new file with the same name will be created. You're SOL
Is a while loop a pretest or post-test loop?
Pre test
The statements in the body of a do-while loop are executed
at least once
The statement or block of a loop that is repeated is known as the_______ of the loop.
body
The ____ statement causes a loop to terminate immediately.
break
The ____ statement causes a loop to skip the remaining statements in the current iteration.
continue
loop
control structure that causes a statement or group of statements to repeast
A(n) is a variable that "counts" the number of times a loop repeats.
counter
5.4 In the following program segment, which variable is the counter and which is the accumulator? int number, maxNums, x = 0, t = 0; cout << "How many numbers do you wish to enter? "; cin >> maxNums; while (x < maxNums) { cout << "Enter the next number: "; cin >> number; t += number; x++; } cout << "The sum of those numbers is " << t << endl;
counter = x accumulator = t
To ________ a value means to decrease it by one.
decrement
To use an output file in a C++ program you must
do create a file stream object that will "point to" (i.e. reference) the file and open the file, but not make sure the file already exists.
The loop always iterates at least once.
do-while loop
iteration
each execution of the loop body
priming read
first value for loop to test
Write a for loop that displays the following set of numbers: 0 10 20 30 40 50 . . . 1000
for(int num = 0; num <= 1000; num += 10) cout << num << " ";
What data type do you use when you want to create a file stream object that can read data from a file?
ifstream
To______ a value means to increase it by one.
increment
A loop that does not have a way of stopping is a(n) loop.
infinite
How many lines will be displayed? int count = 1; while (count < 5); { cout << "My favorite day is Sunday \n"; count = count + 1; }
infinite creates a null statement
How many lines will be displayed? int count = 1; while (count < 5) cout << "My favorite day is Sunday \n"; count = count + 1;
infinite no { } will create an infinite loop
Give the outcome: while(count <= 5);
infinite loop! ; creates a null statement
Inside the for loop's parentheses, the first expression is the ____ , the second expression is the ____ , and the third expression is the _____.
initialization test update
What does the following demonstrate? cout << "enter 1-100"; cin >> number; while ((number <1 || (number > 100)) { cout << "error! enter 1-100"; cin >> number; }
input validation
Write a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The user should be asked if he or she wishes to perform the operation again. If so, the loop should repeat; otherwise it should terminate.
int num1, num2, char again; do { //get two numbers from user cout << "enter two numbers: "; cin >> num1 >> num2; //add numbers and display sum cout << num1 + num2 << endl; //ask user if they want to do it again; yes, repeat. no, terminate cout << "would you like to repeat? enter 'Y' or 'y' for yes. " << endl; cin >> again; } while(again == 'Y' || again == 'y');
Write code that has the user enter a number and stores it in a variable named start. It should then use a while loop to display the double of each number from start through 50.
int start, num; cout << "enter a number: "; cin >> start; num = start; while (num <= 50) { cout << num * 2 <<" "; num++; }
Write this as a do-while loop: int x = 1; while (x < 0) cout << x << endl;
int x = 1; do cout << x << endl; while (x<0)
Each repetition of a loop is known as a(n) .
iteration
A loop that is inside another is called a(n) loop.
nested loop
What data type do you use when you want to create a file stream object that can write data to a file?
ofstream
If a while loop has no braces around the body of the loop
only one statement will execute causing an infinite loop
A loop that evaluates its test expression after each repetition is a(n) loop.
post-test
When the increment or decrement operator is placed after the operand (or to the operand's right), the operator is being used in ______ mode.
postfix
When the increment or decrement operator is placed before the operand (or to the operand's left), the operator is being used in _______ mode.
prefix
A loop that evaluates its test expression before each repetition is a(n) loop.
pretest
A(n) is a sum of numbers that accumulates with each iteration of a loop.
running total
A(n) is a special value that marks the end of a series of values.
sentinel
pretest loop
tests condition BEFORE each iteration
A for statement contains three expressions: initialization, test, and
update
loop control variable
variable that controls the number of times the loop iterates
The While Loop structure
while (condition) { statement; statement; } **no ; after condition
The ___ and ____ loops will not iterate at all if their test expressions are false to start with.
while and for
For data validation, it is best to use a(n)
while loop