Docsity

Understanding Flow Control, Boolean Data Type, and Control Structures in C++, Slides of Advanced Computer Programming

Partial preview of the text

Download Understanding Flow Control, Boolean Data Type, and Control Structures in C++ and more Slides Advanced Computer Programming in PDF only on Docsity!

Chapter 5

Conditions,

Logical

Expressions,

and Selection

Control

Structures

Chapter 5 Topics

  • Data Type bool
  • Using Relational and Logical Operators to

Construct and Evaluate Logical Expressions

  • If-Then-Else Statements
  • If-Then Statements
  • Nested If Statements for Multi-way Branching
  • Testing the State of an I/O Stream
  • Testing a C++ Program

Flow of Control

  • Sequential unless a “control structure” is used to

change the order

  • Two general types of control structures

Selection (also called branching)

Repetition (also called looping)

bool Data Type

  • Type bool is a built-in type consisting of just 2

values, the constants true and false

  • We can declare variables of type bool

bool hasFever; // true if has high temperature

bool isSenior; // true if age is at least 55

Expressions

Control structures use logical expressions to make

choices, which may include:

6 Relational Operators

3 Logical Operators

6 Relational Operators

are used in expressions of form:

ExpressionA Operator ExpressionB

temperature > humidity rain >= average B * B - 4.0 * A * C < 0. hours <= 40 abs (number) == 35 initial != ‘Q’

Comparing Strings

  • Two objects of type string (or a string object and a C string) can be compared using the relational operators
  • A character-by-character comparison is made using the ASCII

character set values

  • If all the characters are equal, then the 2 strings are equal. Otherwise, the string with the character with smaller ASCII value is the “lesser” string

string myState;

string yourState;

myState = “Texas”;

yourState = “Maryland”;

Expression Value

myState == yourState false

myState > yourState true myState == “Texas” true myState < “texas” true

Logical

Expression Meaning Description

! p NOT p! p is false if p is true ! p is true if p is false

p && q p AND q p && q is true if both p and q are true. It is false otherwise.

p || q p OR q p || q is true if either p or q or both are true. It is false otherwise.

int age; bool isSenior, hasFever; float temperature;

age = 20; temperature = 102.0; isSenior = (age >= 55); // isSenior is false hasFever = (temperature > 98.6); // hasFever is true

Expression Value isSenior && hasFever false isSenior || hasFever true ! isSenior true ! hasFever false

“Short - Circuit” Evaluation

  • C++ uses short circuit evaluation of logical

expressions

  • This means logical expressions are evaluated left to

right and evaluation stops as soon as the final truth

value can be determined

Short-Circuit Example

int age, height;

age = 25;

height = 70;

Expression

(age > 50) && (height > 60)

false

Evaluation can stop now because result of && is only true when both sides are true; thus it is already determined that the expression will be false

What happens?

int age, weight;

age = 25;

weight = 145;

Expression

(weight < 180) && (age >= 20)

true Must still be evaluated because truth value of entire expression is not yet known (Why?)

What happens?

int age, height;

age = 25;

height = 70;

Expression

! (height > 60) || (age > 50)

true

false Does this part need to be evaluated?