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

An introduction to flow control in c++ programming, focusing on if-then-else statements, boolean data type, and control structures. It covers topics such as sequential execution, selection control structures, and loop control structures. The document also explains the use of logical expressions, relational operators, and short-circuiting in c++.

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banamala
banamala 🇮🇳

4.4

(19)

114 documents

1 / 83

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5
Conditions,
Logical
Expressions,
and Selection
Control
Structures
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53

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?