Main Page

Previous Page
Next Page

[Page 228]


  • In C++, it is more precise to call a declaration that also reserves memory a definition.

  • The for repetition statement handles all the details of counter-controlled repetition. The general format of the for statement is

          for ( initialization; loopContinuationCondition; increment )

    where initialization initializes the loop's control variable, loopContinuationCondition is the condition that determines whether the loop should continue executing and increment increments the control variable.

  • Typically, for statements are used for counter-controlled repetition and while statements are used for sentinel-controlled repetition.

  • The scope of a variable specifies where it can be used in a program. For example, a control variable declared in the header of a for statement can be used only in the body of the for statementthe control variable will be unknown outside the for statement.

  • The initialization and increment expressions in a for statement header can be comma-separated lists of expressions. The commas, as used in these expressions, are comma operators, which guarantee that lists of expressions evaluate from left to right. The comma operator has the lowest precedence of all C++ operators. The value and type of a comma-separated list of expressions is the value and type of the rightmost expression in the list.

  • The initialization, loop-continuation condition and increment expressions of a for statement can contain arithmetic expressions. Also, the increment of a for statement can be negative, in which case it is really a decrement and the loop counts downward.

  • If the loop-continuation condition in a for header is initially false, the body of the for statement is not performed. Instead, execution proceeds with the statement following the for.

  • Standard library function pow( x, y ) calculates the value of x raised to the yth power. Function pow takes two arguments of type double and returns a double value.

  • Parameterized stream manipulator setw specifies the field width in which the next value output should appear. The value is right justified in the field by default. If the value to be output is larger than the field width, the field width is extended to accommodate the entire value. Nonparameterized stream manipulator left (found in header <iostream>) can be used to cause a value to be left justified in a field and right can be used to restore right justification.

  • Sticky settings are those output-formatting settings that remain in effect until they are changed.

  • The do...while repetition statement tests the loop-continuation condition at the end of the loop, so the body of the loop will be executed at least once. The format for the do...while statement is

        } while ( condition );
  • The switch multiple-selection statement performs different actions based on the possible values of a variable or expression. Each action is associated with the value of a constant integral expression (i.e., any combination of character constants and integer constants that evaluates to a constant integer value) that the variable or expression on which the switch is based may assume.

  • The switch statement consists of a series of case labels and an optional default case.

  • The cin.get() function reads one character from the keyboard. Characters normally are stored in variables of type char; however, characters can be stored in any integer data type, because they are represented as 1-byte integers in the computer. Thus, a character can be treated either as an integer or as a character, depending on its use.

    [Page 229]
  • The end-of-file indicator is a system-dependent keystroke combination that specifies that there is no more data to input. EOF is a symbolic integer constant defined in the <iostream> header file that indicates "end-of-file."

  • The expression in the parentheses following keyword switch is called the controlling expression of the switch. The switch statement compares the value of the controlling expression with each case label.

  • Listing cases consecutively with no statements between them enables the cases to perform the same set of statements.

  • Each case can have multiple statements. The switch selection statement differs from other control statements in that it does not require braces around multiple statements in each case.

  • The switch statement can be used only for testing a constant integral expression. A character constant is represented as the specific character in single quotes, such as 'A'. An integer constant is simply an integer value. Also, each case label can specify only one constant integral expression.

  • C++ provides several data types to represent integersint, char, short and long. The range of integer values for each type depends on the particular computer's hardware.

  • The break statement, when executed in one of the repetition statements (for, while and do...while), causes immediate exit from the statement.

  • The continue statement, when executed in one of the repetition statements (for, while and do...while), skips any remaining statements in the body of the repetition statement and proceeds with the next iteration of the loop. In a while or do...while statement, execution continues with the next evaluation of the condition. In a for statement, execution continues with the increment expression in the for statement header.

  • Logical operators enable programmers to form complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical NOT, also called logical negation).

  • The && (logical AND) operator ensures that two conditions are both TRue before choosing a certain path of execution.

  • The || (logical OR) operator ensures that either or both of two conditions are true before choosing a certain path of execution.

  • An expression containing && or || operators evaluates only until the truth or falsehood of the expression is known. This performance feature for the evaluation of logical AND and logical OR expressions is called short-circuit evaluation.

  • The ! (logical NOT, also called logical negation) operator enables a programmer to "reverse" the meaning of a condition. The unary logical negation operator is placed before a condition to choose a path of execution if the original condition (without the logical negation operator) is false. In most cases, the programmer can avoid using logical negation by expressing the condition with an appropriate relational or equality operator.

  • When used as a condition, any nonzero value implicitly converts to true; 0 (zero) implicitly converts to false.

  • By default, bool values TRue and false are displayed by cout as 1 and 0, respectively. Stream manipulator boolalpha specifies that the value of each bool expression should be displayed as either the word "true" or the word "false."

  • Any form of control ever needed in a C++ program can be expressed in terms of sequence, selection and repetition statements, and these can be combined in only two waysstacking and nesting.

Previous Page
Next Page