Conditionals - PowerPoint PPT Presentation

About This Presentation
Title:

Conditionals

Description:

to choose an execution path depending on the value of a ... more than the bank balance, then print an error. if today is my birthday, then add one to my age ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 22
Provided by: defau1007
Learn more at: https://www.cs.uno.edu
Category:

less

Transcript and Presenter's Notes

Title: Conditionals


1
Conditionals
2
Conditional Execution
  • A conditional statement allows the computer
  • to choose an execution path depending on the
    value of a variable or expression
  • if the withdrawal is more than the bank balance,
    then print an error
  • if today is my birthday, then add one to my age
  • if my grade is greater than 3.5, then give A as
    final grade

3
Conditional statements in C
  • C supports several forms for conditionals
  • if
  • if-else
  • if-elsif-elsif--else
  • And more
  • You as a programmer must decide which form to
    use.
  • General semantics the conditional statement
    depends on a boolean condition, the guard if
    this condition is true the statement that depends
    on it will get executed

4
Conditional Statement in C
  • if (condition)
  • statement
  • The statement is executed if and only if the
    condition is true.
  • if (x lt 100)
  • x x 1
  • if (withdrawalAmount gt balance)
  • printf( NSF check.\n)
  • if (temperature gt 98.6)
  • printf(You have a fever.\n)

5
Conditional statements in C
  • if (condition)
  • statement1
  • else
  • statement2
  • Semantics if condition is true, statement1 gets
    executed, otherwise statement2 gets executed.
  • if (value gt 70)
  • passed passed 1
  • else
  • notPassed notPassed 1

6
Conditional statements in C
  • if (condition1)
  • statement1
  • else if (condition2)
  • statement2
  • else if (condition3)
  • statement3
  • else if (conditionK)
  • statementK
  • else
  • statement

The first condition that is true (from top to
bottom) will make its statement execute. If all
conditions are false last statement executes.
7
Conditional Example
  • if (grade gt 90)
  • countAs countsAs 1
  • else if (grade gt 80)
  • countBs countBs 1
  • else if (grade gt 70)
  • countCs countCs 1
  • else if (grade gt 60)
  • countDs countDs 1
  • else
  • countFs countFs 1

8
Flow of execution
  • x y 10 (s1)
  • if ( x lt 100)
  • x x 1 (s2)
  • y y 10 (s3)
  • S2 depend on the condition (X lt 10)
  • S1 and S3 do not depend on the condition (x lt 10)

Executes s1
true
xlt100
executes s2
false
Does not execute s2 Executes s3
9
Conditional expressions
  • Also called "logical" or "Boolean" expressions
  • Made up of variables, constants, arithmetic
    expressions, and the "relational operators"
  • Math symbols lt , ,gt , , ,
  • in C lt , lt, gt , gt , , !
  • Conditional expression examples
  • airTemperature gt 0.0
  • 98.6 lt body_temperature
  • maritalStatus M
  • divisor ! 0

10
Value of conditional expressions
  • In C conditional expressions are evaluated to
    yield an integer.
  • If the resulting integer value is 1, it is
    equivalent to TRUE.
  • If the resulting integer value is 0 it is
    equivalent to FALSE.
  • An arithmetic relational operator ( lt, lt, gt,
    gt, !) will yield 1 when the relation is true
    and 0 when false.
  • Any integer expression can be used as a
    conditional expression
  • If its value is not zero, it is true
  • Otherwise it its false.
  • Thus we can write
  • if (value) equivalent to if (value !0)
  • statement statement

11
Formation of complex conditional expressions
  • Boolean operators
  • stands for the and
  • stands for the or
  • ! Stands for the negation operator Not
  • Examples
  • (x gt 0) (x lt10)
  • (ylt 0) (y gt 10)
  • !(x 0)
  • ! ( (xgt0) (x lt10))

12
Boolean Tables for , , !
  • Be1 be2 be1 be1
  • 1 1 1 1 1
    1
  • 1 0 0 1 1
    0
  • 0 0 1 0 1
    1
  • 0 0 0 0 0
    0
  • !0 yields 1
  • !1 yields 0

13
DeMorgans Laws
  • ! ( be1 be2) (!be1) (!2be)
  • ! ( be1 be2) (!be1) (!2be)

14
Precedence rules of expressions with boolean
operators
  • Boolean ! Has higher precedence than
  • has higher precedence than
  • , and are left associative.
  • !x y equivalent to (!x) y
  • y z u equivalent to (y z) u
  • z1 z2 z3 equivalent to (z1 z2) z3
  • z1 z2 z3 equivalent to (z1 z2) z3
  • ! Has higher precedence than lt, gt, lt, gt
  • , have lower precedence than lt, gt, lt, gt
  • !x lt y equivalent to (!x) lt y
  • X y lt z equivalent to x (y lt z)

15
Warning
  • Notice that when we write
  • if (be)
  • statement1
  • statement2
  • the boolean expression is only affecting
    statement1. Statement2 will always execute after
    the if is executed.
  • What if we need to perform more than one
    statement based on a condition?

16
Blocks
  • Use a block to form a group of statements, which
    will groups together statements so that they are
    treated as a single statement
  • statement1
  • statement2
  • .
  • With this notion we can write more complex
    conditional statements.

17
Example
  • If (withdrawal lt balance)
  • balance balance - withdrawal
  • monthyCharge monthlyCharge 1.0
  • Else
  • monthlyPenalty monthlyPenalty 25.0
  • To note
  • A single statement ends with semicolon
  • A block does not have semicolon after
  • A block statement can have zero or more
    statements
  • Its best to use block statements in
    conditionals, it helps you reduces the number of
    errors

18
Pitfalls with ifs
  • Using instead of
  • It will not yield a compilation error
  • And accompaning statement might or might not
    execute.
  • In any case, it is a BUG hard to find!!!!!!!!!!!
  • if ( value average)
  • statement

19
Pitfalls with ifs
  • Althought the following statement will not give a
    syntactic error it is incorrect
  • if ( low lt value lt high)
  • value value 2
  • You should write it as
  • if (low lt value value lt high)
  • value value 2

20
Pitfalls with ifs
  • the operator is different from
  • the operator is different from
  • and are not boolean operators!!!!!
  • if used by mistake, you will get no syntax
    errors, but program will most likely produce
    incorrect results.

21
Pitfalls with ifs
  • When using doubles do not compare them for
    equality () or inequality (!)
  • When using either of these two operators with
    doubles you might get incorrect results due to
    accuracy of doubles!
  • 30.0 (1.0/3.0) 10.0
  • May yield false!
  • 1 ½. 1/3. ¼. .. 1/n. Will most likely
    not be equal to 1/n. 1/(n.-1) ½. 1
    for a large value of n.
Write a Comment
User Comments (0)
About PowerShow.com