Boolean Algebra, Selection If Statement - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Boolean Algebra, Selection If Statement

Description:

A rectangle has three sides. The instructor has a pleasant smile ... if-else statements 'glued' together. Switch statement (next week) An advanced construct ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 23
Provided by: Cha356
Category:

less

Transcript and Presenter's Notes

Title: Boolean Algebra, Selection If Statement


1
Boolean Algebra, Selection If Statement
2
Boolean Algebra
  • Logical expressions have the one of two values
    true or false
  • A rectangle has three sides
  • The instructor has a pleasant smile
  • The three basic logical operations are AND, OR,
    NOT

3
Boolean Algebra
  • Three key logical operators in C
  • - logical and
  • - logical or
  • ! - logical not

4
Boolean Algebra
  • Truth tables
  • Lists all combinations of operand values and the
    result of the operation for each combination
  • Example truth table for (logical and)

5
Boolean Algebra
  • Truth table for (logical or)

6
Boolean Algebra
  • Truth table for ! (logical not)

P ! P
False True True False
7
Boolean Algebra
  • Can create complex logical expressions by
    combining simple logical expressions
  • Example
  • ! (P Q)
  • A truth table can be used to determine when a
    logical expression is true

P Q P Q ! (P Q)
False False False True False
True False True True False False True True
True True False
8
A Boolean Type
  • C contains a type named bool
  • Type bool has two symbolic constants
  • true
  • false
  • Boolean operators
  • - logical and
  • - logical or
  • ! - logical not
  • Warning
  • and are also operators so be careful what you
    type

if ( voltage lt 48 milliamp lt 10 )
if ( gpa gt 3.5 class_rank lt 25 )
if ( ! done )
9
A Boolean Type
  • Example logical expressions
  • bool P truebool Q falsebool R
    truebool S (P Q)bool T ((!Q)
    R)bool U !(R (!Q))

10
Relational Operators
  • The condition in the if statement is based on the
    comparison of two items and is usually expressed
    with on of the following symbols

11
Relational Operators
  • Equality operators
  • equal to
  • ! not equal to
  • Examples
  • int i 32
  • int k 45
  • bool q (i k)
  • bool r (i ! k)

12
Relational Operators
  • Ordering operators
  • Examples
  • int i 5
  • int k 12
  • bool p (i lt 10)
  • bool q (k gt i)
  • bool r (i gt k)
  • bool s (k lt 12)

lt gt gt (?) lt (?)
13
Operator Precedence Expanded
  • precedence of operators (from highest to lowest)
  • ()
  • Unary -
  • /
  • -
  • gt lt gt gt !

14
If Statements
15
Selection IF Statements
  • The selection structure represents the decision
    making abilities of the computer.
  • Provide
  • Ability to control whether a statement list is
    executed
  • If statement
  • if
  • if-else
  • if-else-if

16
The Basic If Statement
  • Syntax
  • if (Expression)
  • Action
  • If the Expression is true then execute Action
  • Action is either a single statement or a group of
    statements within braces

Expression
true
false
Action
17
The Basic If Statement Example
if (Value lt 0) Value -Value
18
The If-Else Statement
  • Syntax
  • if (Expression)
  • Action1 else Action2
  • If Expression is true then executeAction1
    otherwise execute Action2
  • if (v 0)
  • cout ltlt "v is 0"
  • else cout ltlt "v is not 0"

Expression
false
true
Action1
Action2
19
Example Finding the Max
  • int Value1
  • int Value2
  • int Max
  • cout ltlt "Enter two integers "
  • cin gtgt Value1 gtgt Value2
  • if (Value1 lt Value2)
  • Max Value2
  • else
  • Max Value1
  • cout ltlt "Maximum of inputs is " ltlt Max ltlt endl

20
Example Finding the Max
21
The If-Else-If Statement
  • It is often the case that depending upon the
    value of an expression we want to perform a
    particular action
  • Two major ways of accomplishing this choice
  • if-else-if statement
  • if-else statements glued together
  • Switch statement (next week)
  • An advanced construct

22
Example Finding Vowels
  • if ((ch 'a') (ch 'A)) cout ltlt ch ltlt
    " is a vowel" ltlt endlelse if ((ch 'e')
    (ch 'E")) cout ltlt " ch ltlt " is a vowel" ltlt
    endlelse if ((ch 'i') (ch 'I")) cout
    ltlt ch ltlt " is a vowel" ltlt endl else if ((ch
    'o') (ch 'O")) cout ltlt ch ltlt " is a vowel"
    ltlt endl else if ((ch 'u') (ch
    'U")) cout ltlt ch ltlt " is a vowel" ltlt endl
  • else cout ltlt ch ltlt " is not a vowel" ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com