Chapter Five

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Nested Loops p.263

A nested loop is a loop that appears inside another loop.

A sentinel is a special value that marks the end of a list of values.

A sentinel signals that there are no more values to be entered. When the sentinel is entered the loop terminates.

In general, there are two types of files: text and binary.

A text file contains data that has been encoded as text, using a scheme such as ASCII or Unicode.

Using break in a Nested Loop

In a nested loop, the break statement only interrupts the loop it is placed in.

Using Multiple Statements in the Initialization and Update Expressions

It is possible to execute more than one statement in the initialization expression for (x = 1, y = 1; x <= 5; x++, y++) This update expression increments BOTH the x and y variables.

Using do-while with Menus p.245

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

A running total is a sum of numbers that accumulates with each iteration of a loop.

The variable used to keep the running total is called an accumulator .

Using Files for Data Storage

When a program needs to save data for later use, it writes the data in a file. The data can then be read from the file at a later time.

How to interpret a for Loop for (int num = 5; num <= 10; num++) cout << num << endl; This will output the numbers 5 through 10

for (int num = 5; num <= 10; num++) The counter num is initialized to the value of 5; The statement AFTER this line will iterate UNTIL the counter is GREATER than 10. In this case it will iterate 6 times.

Often, when opening a file, you will need to specify its path as well as its name.

inputFile.open("C:\\data\\inventory.txt") C:\data\inventory.txt is opened and linked with inputFile.

A counter num++

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

File Access Methods sequential access: direct access aka. random access file A file stream object, a file can be thought of as a stream of data.

sequential access: you access data from the beginning of the file to the end of the file. direct access: can jump directly to any piece of data in the file without reading the data that comes before it.

if a string object named filename is passed as an argument to the open function inputFile.open(filename) Older versions will need the c_str() member function inputFile.open(filename.c_str());

versions prior to C++ 11, a file stream object's open member function will not accept a string object as an argument. The open member function requires that you pass the name of the file as a null-terminated string, which is also known as a C-string . String literals are stored in memory as null-terminated C-strings, but string objects are not.

Using the while Loop for Input Validation

while (number < 1 || number > 100) { cout << "ERROR: Enter a value in the range 1-100: "; cin >> number; }

Increment and Decrement Operators

++ and −− are operators that add and subtract 1 from their operands. ++num or num++, --num or num--, used in prefix the operator works before the variable is used. used in postfix the operator works after. x = 1 y = 2 x + ++y = 4 (Now y = 3 After being incremented)

The for Loop is ideal for performing a known number of iterations. It is a pretest loop. The counter may be initialized outside of the loop's parentheses prior. Do NOT modify the counter in the body of the loop. A count-controlled loop must possess three elements:

1. Initialize a counter variable to a starting value. 2. Test the counter variable by comparing it to a maximum value. When the counter variable reaches its maximum value, the loop terminates. 3. It must update the counter variable during each iteration. Usually by incrementing the variable.

Loops p.232 Each repetition of a loop is known as an iteration. Loops MUST have something inside the body to eventually make the test expression false. In general, there are two categories of loops: conditional loops and count-controlled loops.

A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. The difference between these structures is how they control the repetition.

The while loop. The while loop is a conditional loop, which means it repeats as long as a particular condition exists. It is also a pretest loop, so it is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning.

For example, validating input that has been read and reading lists of data terminated by a sentinel value are good applications of the while loop.

Breaking and Continuing a Loop WARNING! Use the break and continue statements with great caution. Because they bypass the normal condition that controls the loop's iterations, these statements make code difficult to understand and debug. For this reason, you should avoid using break and continue whenever possible.

The break statement causes a loop to terminate early. The continue statement causes a loop to stop its current iteration and begin the next one.

Using Increment and Decrement Operators in Relational Expressions

The difference between postfix and prefix is critical. x = 10 (x++ > 10) is FALSE x is tested BEFORE being incremented (++x > 10) is TRUE x is tested AFTER being incremented

The do-while loop. The do-while loop is also a conditional loop. Unlike the while loop, however, do-while is a posttest loop. It is ideal in situations where you always want the loop to iterate at least once.

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

The do-while Loop The do-while loop must be terminated with a semicolon.

The do-while loop is a posttest loop, which means its expression is tested after each iteration. it always completes at least one iteration.

The for loop is a pretest loop that has built-in expressions for initializing, testing, and updating. These expressions make it very convenient to use a counter variable to control the number of iterations that the loop performs. The initialization expression can initialize the counter variable to a starting value, the test expression can test the counter variable to determine whether it holds the maximum value, and the update expression can increment the counter variable.

The for loop is ideal in situations where the exact number of iterations is known.

The while Loop Begins with a loop header. It consists of the key word while followed by an expression enclosed in parentheses. the expression is tested, and if it is true, the statement is executed. This cycle repeats until the expression is false. The while Loop Is a Pretest Loop. Only if the expression is TRUE will the loop ever iterate (run).

The while loop has two important parts: (1) an expression that is tested for a true or false value (Boolean expression) (2) a statement or block that is repeated as long as the expression is true. The statement that is repeated is known as the body of the loop, it MUST be contained within braces.

A file's read position marks the location of the next byte that will be read from the file.

When an input file is opened, its read position is initially set to the first byte in the file.

The continue Statement The continue statement causes the current iteration of a loop to end immediately continue passes control to the next iteration of a for or while loop. It skips any remaining statements in the body of the loop for the current iteration. The program continues execution from the next iteration. continue applies only to the body of the loop where it is called. In nested loops, continue skips remaining statements only in the body of the loop in which it occurs

When continue is encountered, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration. In a while loop, this means the program jumps to the test expression at the top of the loop. As usual, if the expression is still true, the next iteration begins. In a do-while loop, the program jumps to the test expression at the bottom of the loop, which determines whether the next iteration will begin. In a for loop, continue causes the update expression to be executed and then the test expression to be evaluated.

Nested do-while loop inside of a do-while loop

do { statement(s); // you can put more statements. do { statement(s); }while( condition ); }while( condition );


Ensembles d'études connexes

The Hunger Games Vocabulary: Chapters 20-27

View Set

Chapter 24 An Affluent Society, 1953-1960

View Set

Ch 11. Optimal Portfolio Choice and Capital Asset Pricing Model

View Set

Psych of Aging Ch 8 personality study guide

View Set