CONTROL STRUCTURES (SELECTION)
Objectives
In this chapter, you will:
• Learn about control structures
• Examine relational and logical operators
• Explore how to form and evaluate logical
  (Boolean) expressions
• Discover how to use the selection control
  structures if, if...else, and switch in a
  program
                                              2
Control Structures
• A computer can proceed:
  – In sequence
  – Selectively (branch) - making a choice
  – Repetitively (iteratively) - looping
• Some statements are executed only if certain
  conditions are met
• A condition is met if it evaluates to true


                                                 3
Control Structures (cont.)




                             4
Relational Operators
• A condition is represented by a logical
  (Boolean) expression that can be true or
  false
• Relational operators:
  – Allow comparisons
  – Require two operands (binary)
  – Evaluate to true or false



                                             5
Relational Operators (cont.)




                               6
Relational Operators (cont.)
• You can use the relational operators with all
  three simple data types:
  – 8 < 15 evaluates to true
  – 6 != 6 evaluates to false
  – 2.5 > 5.8 evaluates to false
  – 5.9 <= 7.5 evaluates to true




                                                  7
Relational Operators (cont.)




                               8
Logical (Boolean) Operators and
          Logical Expressions
• Logical (Boolean) operators enable you to
  combine logical expressions


           unary

           binary

           binary




                                              9
Logical (Boolean) Operators and
   Logical Expressions (cont.)




                                  10
11
12
Order of Precedence

• Relational and logical operators are
  evaluated from left to right
• The associativity is left to right
• Parentheses can override precedence




                                         13
Order of Precedence (cont.)




                              14
Order of Precedence (cont.)




                              15
Order of Precedence (cont.)




                              16
Order of Precedence (cont.)




                              17
Selection: if and if...else
•   One-Way Selection
•   Two-Way Selection
•   Compound (Block of) Statements
•   Multiple Selections: Nested if
•   Comparing if...else Statements with a
    Series of if Statements



                                            18
Selection: if and if...else (cont.)
• The syntax of one-way selection is:



• The statement is executed if the value of the
  expression is true
• The statement is bypassed if the value is false;
  program goes to the next statement
• if is a reserved word

                                                     19
One-Way Selection (cont.)




                            20
Two-Way Selection
• Two-way selection takes the form:




• If expression is true, statement1 is executed;
  otherwise, statement2 is executed
   – statement1 and statement2 are any C++ statements
• else is a reserved word


                                                    21
Two-Way Selection (cont.)




                            22
Two-Way Selection (cont.)




                            23
Compound (Block of) Statement
• Compound statement (block of statements):




• A compound statement is a single statement

                                               24
Compound (Block of) Statement
(cont.)
 if (age >   18)
 {
   cout <<   "Eligible to vote." << endl;
   cout <<   "No longer a minor." <<
   endl;
 }
 else
 {
   cout <<   "Not eligible to vote." <<
   endl;
   cout <<   "Still a minor." << endl;
 }
                                            25
Multiple Selections: Nested if
• Nesting: one control statement in another
• An else is associated with the most recent if that
  has not been paired with an else




                                                       26
27
Comparing if…else Statements with
     a Series of if Statements




                                28
Confusion Between == and =
• C++ allows you to use any expression that can
  be evaluated to either true or false as an
  expression in the if statement:
  if (x = 5)
      cout << "The value is five." << endl;
• The appearance of = in place of == resembles
  a silent killer
  – It is not a syntax error
  – It is a logical error

                                                 29
switch Structures
• switch structure: alternate
  to if-else
• switch (integral) expression
  is evaluated first
• Value of the expression
  determines which
  corresponding action is taken
• Expression is sometimes
  called the selector


                                  30
31
switch Structures (cont.)
• One or more statements may follow a case
  label
• Braces are not needed to turn multiple
  statements into a single compound statement
• The break statement may or may not appear
  after each statement
• switch, case, break, and default are
  reserved words


                                            32
33
Summary
• Control structures alter normal control flow
• Most common control structures are selection and
  repetition
• Relational operators: ==, <, <=, >, >=, !=
• Logical expressions evaluate to 1 (true) or 0
  (false)
• Logical operators: ! (not), && (and), || (or)
• Two selection structures: one-way selection and
  two-way selection
• The expression in an if or if...else structure is
  usually a logical expression

                                                      34
Summary (continued)
• No stand-alone else statement in C++
   – Every else has a related if
• A sequence of statements enclosed between braces,
  { and }, is called a compound statement or block of
  statements
• Using assignment in place of the equality operator
  creates a semantic error
• switch structure handles multiway selection
• break statement ends switch statement
 Source:
 C++ Programming: From Problem Analysis to Program Design,
 Fourth Edition                                              35

Control structures selection

  • 1.
  • 2.
    Objectives In this chapter,you will: • Learn about control structures • Examine relational and logical operators • Explore how to form and evaluate logical (Boolean) expressions • Discover how to use the selection control structures if, if...else, and switch in a program 2
  • 3.
    Control Structures • Acomputer can proceed: – In sequence – Selectively (branch) - making a choice – Repetitively (iteratively) - looping • Some statements are executed only if certain conditions are met • A condition is met if it evaluates to true 3
  • 4.
  • 5.
    Relational Operators • Acondition is represented by a logical (Boolean) expression that can be true or false • Relational operators: – Allow comparisons – Require two operands (binary) – Evaluate to true or false 5
  • 6.
  • 7.
    Relational Operators (cont.) •You can use the relational operators with all three simple data types: – 8 < 15 evaluates to true – 6 != 6 evaluates to false – 2.5 > 5.8 evaluates to false – 5.9 <= 7.5 evaluates to true 7
  • 8.
  • 9.
    Logical (Boolean) Operatorsand Logical Expressions • Logical (Boolean) operators enable you to combine logical expressions unary binary binary 9
  • 10.
    Logical (Boolean) Operatorsand Logical Expressions (cont.) 10
  • 11.
  • 12.
  • 13.
    Order of Precedence •Relational and logical operators are evaluated from left to right • The associativity is left to right • Parentheses can override precedence 13
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    Selection: if andif...else • One-Way Selection • Two-Way Selection • Compound (Block of) Statements • Multiple Selections: Nested if • Comparing if...else Statements with a Series of if Statements 18
  • 19.
    Selection: if andif...else (cont.) • The syntax of one-way selection is: • The statement is executed if the value of the expression is true • The statement is bypassed if the value is false; program goes to the next statement • if is a reserved word 19
  • 20.
  • 21.
    Two-Way Selection • Two-wayselection takes the form: • If expression is true, statement1 is executed; otherwise, statement2 is executed – statement1 and statement2 are any C++ statements • else is a reserved word 21
  • 22.
  • 23.
  • 24.
    Compound (Block of)Statement • Compound statement (block of statements): • A compound statement is a single statement 24
  • 25.
    Compound (Block of)Statement (cont.) if (age > 18) { cout << "Eligible to vote." << endl; cout << "No longer a minor." << endl; } else { cout << "Not eligible to vote." << endl; cout << "Still a minor." << endl; } 25
  • 26.
    Multiple Selections: Nestedif • Nesting: one control statement in another • An else is associated with the most recent if that has not been paired with an else 26
  • 27.
  • 28.
    Comparing if…else Statementswith a Series of if Statements 28
  • 29.
    Confusion Between ==and = • C++ allows you to use any expression that can be evaluated to either true or false as an expression in the if statement: if (x = 5) cout << "The value is five." << endl; • The appearance of = in place of == resembles a silent killer – It is not a syntax error – It is a logical error 29
  • 30.
    switch Structures • switchstructure: alternate to if-else • switch (integral) expression is evaluated first • Value of the expression determines which corresponding action is taken • Expression is sometimes called the selector 30
  • 31.
  • 32.
    switch Structures (cont.) •One or more statements may follow a case label • Braces are not needed to turn multiple statements into a single compound statement • The break statement may or may not appear after each statement • switch, case, break, and default are reserved words 32
  • 33.
  • 34.
    Summary • Control structuresalter normal control flow • Most common control structures are selection and repetition • Relational operators: ==, <, <=, >, >=, != • Logical expressions evaluate to 1 (true) or 0 (false) • Logical operators: ! (not), && (and), || (or) • Two selection structures: one-way selection and two-way selection • The expression in an if or if...else structure is usually a logical expression 34
  • 35.
    Summary (continued) • Nostand-alone else statement in C++ – Every else has a related if • A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements • Using assignment in place of the equality operator creates a semantic error • switch structure handles multiway selection • break statement ends switch statement Source: C++ Programming: From Problem Analysis to Program Design, Fourth Edition 35