Chapter 5 Review

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The while loop has two important parts:

(1) an expression that is tested for a true or false value, (2) a statement or block that is repeated as long as the expression is true.

conditional loop

A conditional loop executes as long as a particular condition exists.

counters

A counter is a variable that is regularly incremented or decremented each time a loop iterates.

for loop example

Example: for (initialization; test; update) statement;

The programming style you should use with the while loop is similar to that of the if statement:

If there is only one statement repeated by the loop, it should appear on the line after the while statement and be indented one additional level. If the loop repeats a block, each line inside the braces should be indented.

An important characteristic of the while loop is that the loop will never iterate if the test expression is false to start with.

If you want to be sure that a while loop executes the first time, you must initialize the relevant data in such a way that the test expression starts out as true.

Don't Forget the Braces with a Block of Statements

If you write a loop that conditionally executes a block of statements, don't forget to enclose all of the statements in a set of braces. If the braces are accidentally left out, the while statement conditionally executes only the very next statement.

Using ++ and −− in Relational Expressions

Just as in mathematical expressions, the difference between postfix and prefix modes is critical.

The read operation that takes place just before the loop is called a ________ _________.

Priming read. It provides the first value for the loop to test. Subsequent values are obtained by the loop.

Using do-while with Menus

The do-while loop is a good choice for repeating a menu

The do-while loop

The do-while loop is a posttest loop, which means its expression is tested after each iteration.

The for Loop

The for loop is ideal for performing a known number of iterations.

An lvalue identifies a place in memory whose contents may be changed.

The increment and decrement operators usually have variables for their operands, but generally speaking, anything that can go on the left side of an = operator is legal.

postfix mode

The operator is placed after the variable. The increment or decrement will happen last.

prefix mode,

The operator is placed before the variable. The increment or decrement will happen first.

If the statement were changed to read a = 2; b = 5; c = a * ++b;

The variable b would be incremented before it was multiplied by a. In this case, c would be assigned the value of 2 times 6, so the cout statement would display 2 6 12

Using the while Loop for Input Validation

The while loop can be used to create input routines that repeat until acceptable data is entered.

Loop input validation

This code first allows the user to enter a number. This takes place just before the loop. If the input is valid, the loop will not execute. If the input is invalid, however, the loop will display an error message and require the user to enter another number. The loop will continue to execute until the user enters a valid number.

The do-while loop always performs at least one iteration, even if the expression is false to begin with.

This differs from the behavior of a while loop, which you will recall is a pretest loop.

This type of loop allows the user to decide the number of iterations.

This type of loop is known as a user-controlled loop,

++ and −− are operators that add and subtract 1 from their operands.

True

The while loop is a pretest loop. o True o False

True

Using the for Loop instead of while or do-while

You should use the for loop instead of the while or do-while loop in any situation that clearly requires an initialization, uses a false condition to stop the loop, and requires an update to occur at the end of each loop iteration

Using ++ and −− in Mathematical Expressions

a = 2; b = 5; c = a * b++; cout << a << " " << b << " " << c; In the statement c = a * b++, c is assigned the value of a times b, which is 10. The variable b is then incremented. The cout statement will display 2 6 10

After the key word for, there are three expressions inside the parentheses, separated by semicolons. (Notice there is not a semicolon after the third expression.)

a for loop

Each repetition of a loop is known as an __________.

an iteration

In general, there are two categories of loops:

conditional loops and count-controlled loops.

It's also possible to create an infinite loop by accidentally placing a semicolon after the __________ line of the while loop.

first line of the while loop. Example: while (number < 5); // This semicolon is an ERROR!

example of a for loop

for (count = 0; count < 5; count++) cout << "Hello" << endl; In this loop, the initialization expression is count = 0, the test expression is count < 5, and the update expression is count++.

The format of the for loop when it is used to repeat a block is

for (initialization; test; update) { statement; statement; // Place as many statements here // as necessary. }

Count-controlled loops are so common that C++ provides a type of loop specifically for them. It is known as the _______________ loop.

for loop; The for loop is specifically designed to initialize, test, and update a counter variable.

The first line of the for loop is the loop ___________.

header

C++ provides a set of simple unary operators designed just for incrementing and decrementing variables. The increment operator is ++, and the decrement operator is −−

increment example num++; decrement example num--;

the first expression on a for loop header is ________________

initialization expression; It is normally used to initialize a counter variable to its starting value. This is the first action performed by the loop, and it is only done once

The Difference between Postfix and Prefix Modes

it doesn't matter if the increment or decrement operator is used in postfix or prefix mode. The difference is important, however, when these operators are used in statements that do more than just incrementing or decrementing.

If a loop does not have a way of stopping, it is called an ___________.

it is called an infinite loop. An infinite loop continues to repeat until the program is interrupted. Here is an example of an infinite loop: int number = 0; while (number < 5) { cout << "Hello\n"; } This is an infinite loop because it does not contain a statement that changes the value of the number variable. Each time the expression number < 5 is tested, number will contain the value 0.

You should use the do-while loop when you want to make sure the loop executes at least ___________.

once

To increment a value means to increase it by _____?

one Example: num = num + 1; num += 1;

To decrement a value means to decrease it by _________?

one Example: num = num − 1; num −= 1;

The do-while loop must be terminated with a ___________?

semicolon

When the expression number < 5 is tested and found to be false, the loop will ___________ and the program will resume execution at the statement that immediately follows the loop.

terminate

the second expression for a loop header is ________________

test expression; This is an expression that controls the execution of the loop. As long as this expression is true, the body of the for loop will repeat. The for loop is a pretest loop, so it evaluates the test expression before each iteration.

the third expression for a loop header is _________________

update expression; It executes at the end of each iteration. Typically, this is a statement that increments the loop's counter variable.

Input validation is the process of inspecting data given to a program by the user and determining if it is _________.

valid

The while Loop is known as a Pretest Loop

which means it tests its expression before each iteration.

Here is the general format of the while loop:

while (expression) statement;


Kaugnay na mga set ng pag-aaral

Genetics chp. 16 Cancer Genetics

View Set

CH 38- Assessment and Management of Patients With Rheumatic Disorders: PrepU (CC3 Immunity 3)

View Set

1- 1 الدرس الأول - هل قضيت عطلة سعيدة؟

View Set

normal and binomial distributions

View Set

renal failure and perfusion practice questions

View Set

The Child with Genitourinary Alteration2019

View Set