Chapter 5 Flashcards

1
Q

Increment Operator

A

prefix: val++;
postfix: ++val;
- adds one to a variable
- can be used in expressions: (result = num1++ + –num2)
- can be used in relational expressions (pre/postfix operations will cause different comparisons)

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

Decrement Operator

A

prefix: –val;
postfix: val–;
- subtracts one from a variable
- can be used in expressions: (result = num1++ + –num2)
- can be used in relational expressions (pre/postfix operations will cause different comparisons)

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

prefix vs postfix

A

prefix mode: increments or decrements then returns the value of the variable
postfix mode: returns the value, then increments or decrements

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

Loop

A
  • a control structure that causes a statement or statements to repeat
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

while loop format

A

while (expression) {
statement;
statement;
}

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

how does the while loop work

A

expression is evaluated before the loop executes
- if true, then statement is executed, and expression is evaluated again
- if false, then the loop is finished and program statements following statement execute

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

things to watch out for in loops

A
  • loop must contain code to make expression become false; otherwise, the loop will have no way of stopping - called an infinite loop because it will repeat an infinite number of times
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

using the while loop for input validation

A
  • the while loop can be used to create input routines that reject invalid data, and repeat until valid data is entered.
  • general approach in pseudocode
    read an item of input
    while the input is invalid
    display an error message
    read the input again
    end while
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

counters

A
  • a variable that is incremented or decremented each time a loop repeats
  • can be used to control execution of the loop (AKA: loop control variable)
  • must be initialized before entering loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

do while loop

A
  • a posttest loop, execute the loop, then test the expression
  • always executes at least once
  • execution continues as long as expression is true, stops repeating when expression becomes false
  • useful in menu-driven programs to bring user back to menu selection
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

do while loop format

A

do {
statement;
} while (expression);

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

for loop

A
  • useful for counter-controlled loop
  • pretest loop: tests its test expression before each iteration
  • can have multiple statements in the initialization expressions
  • can have multiple statements in the test expression
  • can omit the initialization expression if var has already been initialized
  • can declare variables in the initialization expression: then. the scope of the variable is the for loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how the for loop works

A

1) perform initialization
2) evaluate test expression
- if true, execute statement
- if false, terminate loop execution
3) execute update, then re-evaluate test expression

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

for loop format

A

for (initialization; test; update) {
statement;
}

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

when to use the for loop

A

in any situation that clearly requires
- an initialization
- a false condition to stop the loop
- an update to occur at the end of each iteration

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

running total

A

accumulated sum of numbers from each repetition of loop

17
Q

accumulator

A

variable that holds running total

18
Q

sentinel

A
  • value in a list of values that indicates end of data
  • special value cannot be confused with a valid value
  • used to terminate input when user may not know many values will be entered
19
Q

deciding which loop to use

A

1) the while loop is a conditional pretest loop
- iterates as long as a certain condition exits
- validating input
- reading lists of data terminated by a sentinel

2) the do-while loop is a conditional posttest loop
- always iterates at least once
- repeating a menu

3) the for loop is a pretest loop
- built-in expressions for initializing, testing, and updating
- situations where the exact number of iterations is known

20
Q

nested loops

A
  • a loop inside the body of another loop
  • inner loop goes through all repetitions for each repetition of outer loop
  • inner loop repetitions complete sooner than outer loop
  • total number of repetitions for inner loop is product of number of repetitions of the two loops
21
Q

nested for loop format

A

for (initialization; test; update) { // outer loop
for (initialization; test; update) { // inner loop
statement;
}
statement;
}

22
Q

files

A
  • can use files instead of keyboard, monitor screen for program input, output
  • allows data to be retained between program runs
  • fstream header file for file access
23
Q

steps for using files

A
  1. open the file
  2. use the file (read from, write to, or both)
  3. close the file
24
Q

file stream types

A
  • ifstream: for input from a file
  • ofstream: for output to a file
  • fstream: for input from or output to a file
25
Q

define file stream objects

A

ifstream infile;
ofstream outfile;

26
Q

opening files

A
  • create a link between file name (outside the program) and file stream object (inside the program) by using the open member function
  • open member function:
    infile.open(“inventory.dat”);
    outfile.open(“report.txt”);
  • filename may include drive, path info
  • output file will be created if necessary; existing file will be erased first
  • input file must exist for ‘open’ to work
27
Q

testing for file open errors

A
  • can test a file stream object to detect if an open operation failed

infile.open(“test.txt”);
if (!infile) {
cout &laquo_space;“File open failure!”;
}

  • can also use the fail member function
28
Q

using files

A
  • can use output file object and &laquo_space; to send data to a file
    outfile &laquo_space; “Inventory report”;
  • can use input file object and&raquo_space; to copy data from file to variables
    infile&raquo_space; partNum;
    infile&raquo_space; qtyInStock&raquo_space; qtyOnOrder;
29
Q

using loops to process files

A
  • the stream extraction operator&raquo_space; returns true when a value was successfully read, false otherwise
  • can be tested in a while loop to continue execution as long as values are read from the file:
    while (inputFile&raquo_space; number) . . .
30
Q

closing files

A
  • use the close member function:
    infile.close();
    outfile.close();
  • dont wait for operating system to close files at program end
    • may be limit on number of open files
    • may be buffered output data waiting to send to file
31
Q

letting user specify a filename

A
  • in many cases, you will want the user to specify the name of a file for the program to open
  • in C++, you can pass a string object as an argument to a file stream object’s open member function
32
Q

c_str in older versions of C++

A
  • prior to c++ 11, the open member function requires that you pass the name of the file as a null-terminated string, AKA C-string
  • string literals are stored in memory as null-terminated C-strings, but string objects are not
  • string objects have a member function named c_str
    • it returns the contents of the object formatted as a null-terminated C-string
    • general format:
      stringObject.c_str()
    • example :
      inputFile.open(filename.c_str());
33
Q

breaking out of a loop

A
  • can use break to terminate execution of a loop
  • use sparingly if at all - makes code harder to understand and debug
  • when used in an inner loop, terminates that loop only and goes back to outer loop
34
Q

the continue statement

A
  • can use continue to go to end of loop and prepare for next repetition
    • while, do-while loops: go to test, repeat loop if test passes
    • for loop: perform update step, then test, then repeat loop if test passes
  • use sparingly - like break, can make program logic hard to follow