Selection in C PowerPoint PPT Presentation

presentation player overlay
1 / 23
About This Presentation
Transcript and Presenter's Notes

Title: Selection in C


1
  • Selection in C

2
If Statement
  • One way decision
  • Referred to as Open Branch in the book.
  • Syntax Flowchart
  • if ( condition)
  • statement(s)

Condition?
true
Statement(s)
false
Rest of the program
3
If / else statement
  • 2-way decision
  • Syntax Flowchart
  • if (condition)
  • statement(s)
  • else
  • statement(s)

Condition?
false
true
Statement(s)
Statement(s)
Rest of the program
4
Exercise Write a C program to..
  • Ask the user to enter his/her age.
  • Check the age, if it is gt 21, display You may
    drink., otherwise, display Do not drink
  • regardless of the age, Display Do not Drink and
    Drive.

5
Explain the solution, using flowchart
  • Flowchart

Start
Declare variable age
Get users age
Age gt 21?
true
false
You may drink
May not drink
Dont drink drive
end
6
Explain the solution using pseudo code
  • Pseudo code
  • Declare variable to store age.
  • Prompt the user to enter age,
  • Read the entered value into variable
  • If age is gt 21 then
  • Display you may drink
  • Otherwise
  • Display you may not drink
  • 5. Display Do not drink and drive

7
Translate the logic into C code
8
Nested If Statement
  • An if statement can be nested in the if part or
    the else part of the if statement.
  • if (condition)
  • if (condition)
  • .
  • else
  • else
  • if (condition)
  • How can you tell where an if statement begins or
    ends?

9
  • Rule else binds with the closest if
  • Ex1 Assume that int a 5, b 7, c 10
  • if (a lt b)
  • if (c lt a b)
  • printf( " First place \n)
  • else
  • printf( "2nd place \n)
  • else
  • printf (3rd place \n)
  • Output

10
Dangling else
  • Determine which if the else binds with?
  • Ex2 Assume a 12, b 2, c 10
  • if (a lt b)
  • if (c lt a b)
  • printf( " First place \n)
  • else
  • printf( "2nd place \n)
  • Output

11
Ex What is the outcome?





  • float score 75
  • float gpa 2.5
  • if (score gt 80)
  • if (gpa gt 2.0)
  • printf (xxx)
  • else
  • printf( )

12
If / else if statement
  • The else part of an if statement can be another
    if statement.
  • if (condition)
  • else
  • if (condition)
  • else
  • if (condition)
  • .
  • else

13
Improved style ? more clarity
  • Such nested if / else can be written as
  • if (condition)
  • else if (condition)
  • else if (condition)
  • .
  • else

14
Example
  • Examine the students test score and assign a
    letter grade
  • int score
  • char letter
  • if (score gt 90)
  • letter A
  • printf( Great job! \n)
  • else if (score gt 80)
  • letter B
  • else
  • if (score gt 70)

15
Rewrite the code with improved style
  • if (score gt 90)
  • letter A
  • printf( Great job! \n)
  • else if (score gt 80)
  • letter B
  • else if (score gt 70)
  • letter C
  • else if (score gt 60)
  • letter D
  • else
  • letter F
  • Printf( Your letter grade is c, letter)

16
More complex conditions
  • You may combine 2 or more conditions using
    logical operators
  • Logical Operators
  • Logical and

17
Application
  • Check the range of the data
  • Ex A healthy heart rate is between 60 and
    85. Check the users heart rate, and inform the
    user whether s/he is healthy or not
  • int rate
  • printf ( Enter your heart rate )
  • Scanf(d, rate)
  • if ((rate gt 60) (rate lt 85) )
  • else

18
Logical or
19
Applications
  • 1) Checking for invalid rage
  • Ex1 Check the range of the test score. If it is
    outside the range of 0 .. 100 end the program
  • int test
  • test 77
  • if ( (test lt 0) (test gt 100) )
  • printf ( Invalid test score..)
  • printf ( Program will end now.)
  • return 1

20
Cont
  • Ex2 Assume that a company is considering a
    promotion for employees older than 60, or those
    who are paid under 20,000. Assume variables
    salary and age are declared with some values
    stored in them.
  • If ( (salary lt 20000) (age gt 60) )
  • printf ( Give promotion..)
  • else
  • printf ( Does not qualify!)

21
Cont
  • Logical not !
  • Operates on one condition.
  • It negates the value of the condition.
  • Ex1 ! (120 lt 100) ?
  • Ex2 int veteran 1
  • !veteran ?
  • Notice
  • ! (a lt b) is the same as (a gt b)
  • ! (a b) is the same as (a ! b)

22
Precedence Rule
  • (..)
  • Unary minus, !
  • /
  • -
  • lt, lt, gt, gt
  • , !
  • assignment operator

23
Evaluate the following expressions
  • Assume int a 5, b 10, c 15, flag 1
  • a lt b b lt c
  • flag b gt 10
  • !(b lt 12) (a 2 0)
  • !flag c gt a
  • int answer
  • answer (a gt b) (c gt b)
  • Printf (d,answer)
  • C gt a b
Write a Comment
User Comments (0)
About PowerShow.com