View page as slide show

Control Structures: Selection

Mithat Konar

Introduction

Control structures in C++

Single-entry/single-exit structures

Selection structures

if selection structure

if syntax

Relational operators

C++ relational operators

Order of precedence

operator associativity type
() left to right parenthesis
* / % left to right multiplicative
+ - left to right additive
<< >> left to right stream insertion/extraction
< <= > >= left to right relational
== != right to left equality
= right to left assignment

if/else selection structure

if/else syntax

Nested if/else selection structure

Nested if/else pseudocode

Nested if/else pseudocode

Nested if/else syntax

Compound statements

Compound statement example

Blocks

Logical operators

C++ logical operators

Truth table for logical AND

expression result
false && false false
false && true false
true && false false
true && true true

Truth table for logical OR

expression result
false || false false
false || true true
true || false true
true || true true

Truth table for logical NOT

expression result
!false true
!true false

Examples

assume: int x = 12, y = 5, z = -4;

expression result
(x > y) && (y > z) true
(x > y) && (z > y) false
(x <= z) || (y == z) false
(x <= z) || (y != z) true
!(x >= z)false

Short-circuiting

More about Boolean values

Comparing characters and strings

switch structure

The conditional operator

More about blocks and scope