IS12 Introduction to Programming Lecture 13: Conditionals - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

IS12 Introduction to Programming Lecture 13: Conditionals

Description:

Work with KnowledgeTree - quizzes, dissections, Expression Executor. Try free C tutorials (KnowledgeSea) Ask questions in the forums ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 21
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: IS12 Introduction to Programming Lecture 13: Conditionals


1
IS12 - Introduction to Programming Lecture 13
Conditionals
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0012-041/

2
Course Logistics
  • We are at a turning point
  • We have got through C basics and programs are
    getting complicated
  • Are you able to follow?
  • How challenging was the exam?
  • Can you understand and write programs?
  • This is the last chance to catch up

3
What you have to do?
  • Attend lectures and browse it again
  • Do your reading assignments
  • Explore all examples - run, modify...
  • Solve all problems (including exercises)
  • Work with KnowledgeTree - quizzes, dissections,
    Expression Executor
  • Try free C tutorials (KnowledgeSea)
  • Ask questions in the forums
  • Come and talk with us, use office hours

4
Outline
  • If and If-else
  • The use of if-else
  • The use of if
  • Simple nested ifs
  • If inside a loop
  • Conditional operator
  • If-else vs. conditional operator

5
Conditional Statement if-else
  • if (expression)
  • statement-1
  • else
  • statement-2
  • nextstatement
  • If expression is not 0 (true) - statement-1
  • If expression is 0 (false) - statement-2
  • In any case after that - nextstatement

6
Flowchart of if-else
Expression ?
! 0 (true)
0 (false)
Statement-1
Statement-2
Nextstatement
7
if-else with blocks
  • if (expression)
  • statement-11
  • ...
  • else
  • statement-21
  • nextstatement

8
Conditional Statement if
  • if (expression)
  • statement-1
  • nextstatement
  • If expression value isnt 0 (true) - statement-1,
    after that - nextstatement
  • If expression value is 0 (false) - nextstatement

9
Flowchart of if
Expression ?
! 0 (true)
0 (false)
Statement1
Statement3
10
if with blocks
  • if (expression)
  • statement-11
  • ...
  • nextstatement

11
Example Maximum with if-else
  • include ltstdio.hgt
  • void main()
  • int a, b
  • printf("Enter two integers ")
  • scanf("d d", a, b)
  • printf("Maximum of d and d ", a, b)
  • if(a lt b)
  • printf("is d\n", b)
  • else
  • printf("is d\n", a)

12
Example Maximum with if
  • include ltstdio.hgt
  • void main()
  • int a, b, max
  • printf("Enter two integers ")
  • scanf("d d", a, b)
  • max a / pre-assignment /
  • if(a lt b)
  • max b / re-assignment /
  • printf("Maximum of d and d is d", a, b, max)

13
Example Variable Rate (1)
  • define THRESHOLD 5000
  • include ltstdio.hgt
  • void main()
  • float rate1, rate2, interest_rate / interest
    rates /
  • float capital / capital /
  • float annual_interest / annual interest /
  • / read data /
  • printf("Interest rates (xx.xx) ")
  • scanf("f f", rate1, rate2)
  • printf("Capital (.cc) ")
  • scanf("f",capital)

14
Example Variable Rate (2)
  • / calculate the rate /
  • if (capital lt THRESHOLD)
  • interest_rate rate1
  • else
  • interest_rate rate2
  • printf("The rate for .2f is .2f\n",
  • capital, interest_rate)
  • / calculate capital /
  • annual_interest capital interest_rate / 100
  • printf("Interest 6.2f New capital 9.2f\n",
    annual_interest, capital annual_interest)

15
Example Nested if - max3
  • include ltstdio.hgt
  • void main()
  • int a, b, c
  • printf("Enter three integers ")
  • scanf("d d d", a, b, c)
  • printf("Maximum of d, d and d is ", a, b, c)
  • if(a gt b)
  • if(a gt c)
  • printf("d\n", a)
  • else
  • printf("d\n", c)
  • else
  • if(b gt c)
  • printf("d\n", b)
  • else
  • printf("d\n", c)

16
Example Running Max
  • define SENTINEL -1
  • include ltstdio.hgt
  • void main ()
  • int max, nextnumber
  • printf("Number ")
  • scanf("d", nextnumber) / read first number
    /
  • max nextnumber / pre-assignment /
  • while (nextnumber ! SENTINEL)
  • if (max lt nextnumber)
  • max nextnumber
  • printf("Number ")
  • scanf("d", nextnumber)
  • printf ("Max d\n", max)

The Running Max pattern Interleaved with The
Sentinel Loop pattern
17
Conditional Operator
  • Expr1 ? Expr2 Expr3
  • Conditional operator calculates the value from 3
    arguments (expressions)
  • First, Expr1 is evaluated
  • If it is true (not zero), then Expr2 is
    calculated its value is the result
  • If it is false (zero), then Expr3 is calculated
    its value is the result

18
Conditional Operator Examples
  • min a lt b ? a b
  • rate capital lt 3000 ? rate1 rate 2
  • printf("c\n", (lower ? 'a' 'A'))
  • x a (b 2 ? b - 1 b)

19
Compare Conditionals
  • Conditional statement
  • Controls the order of statement execution
  • if (capital lt THRESHOLD)
  • interest_rate rate1
  • else
  • interest_rate rate2
  • Conditional operator
  • Calculates a value
  • interest_rate capital lt THRESHOLD ? rate1
    rate2

20
Before next lecture
  • Do reading assignment
  • Perry Chapter 11 Chapter 13 Hello Conditional
  • Run Classroom Examples
  • Exercises variable rate in a loop calculating
    minimum of a sequence
  • Check yourself by working with KnowledgeTree and
    its components
Write a Comment
User Comments (0)
About PowerShow.com