Which of the following is used when you want to execute a sequence of statements based on the results of multiple Boolean expressions?

Control Structures - Intro, Selection

Flow of Control:

Flow of control through any given function is implemented with three basic types of control structures:
  • Sequential: default mode. Sequential execution of code statements [one line after another] -- like following a recipe
  • Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In C++, these are the types of selection statements:
    • if
    • if/else
    • switch
  • Repetition: used for looping, i.e. repeating a piece of code multiple times in a row. In C++, there are three types of loops:
    • while
    • do/while
    • for

The function construct, itself, forms another way to affect flow of control through a whole program. This will be discussed later in the course.

Some useful tools for building programs or program segments

  • pseudocode - helps "think" out a problem or algorithm before trying to code it
  • flowcharting - graphical way to formulate an algorithm or a program's flow
  • stepwise refinement [top-down design] of algorithms

True and False

  • Selection and repetition statements typically involve decision steps. These steps rely on conditions that are evaluated as true or false
  • C++ has a boolean data type [called bool] that has values true and false. Improves readability.
  • Most functions that answer a yes/no question [or a true/false situation] will return a boolean answer [or in the case of user-defined functions, they should be coded that way]
  • Important: ANY C++ expression that evaluates to a value [i.e. any R-value] can be interpreted as a true/false condition. The rule is:
    • If an expression evaluates to 0, its truth value is false
    • If an expression evaluates to non-zero, its truth value is true

Logical Operators:

The arithmetic comparison operators in C++ work much like the symbols we use in mathematics.  Each of these operators returns a true or a false. x == y // x is equal to y x != y // x is not equal to y x < y // x is less than y x y // x is greater than y x >= y // x is greater than or equal to y

We also have Boolean operators for combining expressions.  Again, these operators return true or false

x && y // the AND operator -- true if both x and y are true x || y // the OR operator -- true if either x or y [or both] are true !x // the NOT operator [negation] -- true if x is false

These operators will be commonly used as test expressions in selection statements or repetition statements [loops].
 

Examples of expressions

[x > 0 && y > 0 && z > 0] // all three of [x, y, z] are positive [x < 0 || y < 0 || z < 0] // at least one of the three variables is negative [ numStudents >= 20 && ![classAvg < 70]] // there are at least 20 students and the class average is at least 70 [ numStudents >= 20 && classAvg >= 70] // means the same thing as the previous expression

Short Circuit Evaluation:

  • The && and || operators also have a feature known as short-circuit evaluation.
  • In the Boolean AND expression [X && Y], if X is false, there is no need to evaluate Y [so the evaluation stops]. Example: [d != 0 && n / d > 0] // notice that the short circuit is crucial in this one. If d is 0, // then evaluating [n / d] would result in division by 0 [illegal]. But // the "short-circuit" prevents it in this case. If d is 0, the first // operand [d != 0] is false. So the whole && is false.
  • Similarly, for the Boolean OR operation [X || Y], if the first part is true, the whole thing is true, so there is no need to continue the evaluation. The computer only evaluates as much of the expression as it needs. This can allow the programmer to write faster executing code.

Selection Statements

The if/else Selection Statement

  • The most common selection statement is the if/else statement. Basic syntax: if [expression] statement else statement
  • The else clause is optional, so this format is also legal: if [expression] statement
  • The expression part can be any expression that evaluates a value [an R-value], and it must be enclosed in parintheses [ ].
    • The best use is to make the expression a Boolean expression, which is an operation that evaluates to true or false
    • For other expressions [like [x + y], for instance]:
      • an expression that evaluates to 0 is considered false
      • an expression that evalautes to anything else [non-zero] is considered true
  • The statement parts are the "bodies" of the if-clause and the else-clause. The statement after the if or else clause must be either:
    • an empty statement ;
    • a single statement expression;
    • a compound statement [i.e. a block]. Can enclose multiple code statements. Remember, a compound statement is enclosed in set braces { }
       
  • Appropriate indentation of the bodies of the if-clause and else-clause is a very good idea [for human readability], but irrelevant to the compiler

Examples


if [grade >= 68] cout

Bài Viết Liên Quan

Chủ Đề