Conditional Statements - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Conditional Statements

Description:

Conditional Statements – PowerPoint PPT presentation

Number of Views:656
Avg rating:3.0/5.0
Slides: 25
Provided by: adria9
Category:

less

Transcript and Presenter's Notes

Title: Conditional Statements


1
Conditional Statements
  • if statements
  • if-else statements

2
if Statement
  • Often you will want your program to execute
    certain commands only if a certain condition is
    true. For example

void main(void) double midtermMark,
finalExamMark, averageMark cout ltlt "Enter
marks for the midterm and final exams " cin gtgt
midtermMark gtgt finalExamMark averageMark
midtermMark0.4 finalExamMark0.6 if
(averageMark gt 50.0) cout ltlt "\nStudent
passes with an average mark of " ltlt
averageMark ltlt endl if (averageMark lt 50.0)
cout ltlt "\nStudent fails with an average mark
of " ltlt averageMark ltlt endl
3
C if Syntax
  • if (ltconditiongt)
  • ltexecutable statementsgt
  • If the condition evaluates to true then the
    executable statements enclosed in the brackets
    are executed otherwise, those statements are
    ignored.

Enter marks for midterm and final exams 75
90 Student passes with an average mark of 84
Enter marks for midterm and final exams 55
45 Student fails with an average mark of 49
4
if Flow Chart
ltstatement beforegt
ltstatement beforegt if (ltconditiongt)
ltexecutable statementsgt ltstatement aftergt
false
ltconditiongt
true
ltexecutable statementsgt
ltstatement aftergt
5
if Flow Chart
y x 5
y x 5 if (y gt 100) y 100 cout ltlt y
ltlt endl
y gt 100
false
true
y 100
cout ltlt y ltlt endl
6
if-else Statement
  • A 2-way conditional statement can be made, which
    executes a set of statements if a condition is
    true and a different set of statements if the
    condition is false. This code has the same effect
    as the previous code.

void main(void) double midtermMark,
finalExamMark, averageMark cout ltlt "Enter
marks for the midterm and final exams " cin gtgt
midtermMark gtgt finalExamMark averageMark
midtermMark0.4 finalExamMark0.6 if
(averageMark gt 50.0) cout ltlt "\nStudent
passes with an average mark of " ltlt
averageMark ltlt endl else cout ltlt
"\nStudent fails with an average mark of "
ltlt averageMark ltlt endl
7
C if-else Syntax
  • if (ltconditiongt)
  • ltexecutable statements 1gt
  • else
  • ltexecutable statements 2gt
  • If the condition evaluates to true then the
    executable statements 1 are executed and the
    executable statement 2 are ignored otherwise,
    the executable statements 1 are ignored and the
    executable statements 2 are executed.

8
if-else Flow Chart
ltstatement beforegt
ltstatement beforegt if (ltconditiongt)
ltexecutable statements 1gt else
ltexecutable statements 2gt ltstatement
aftergt
ltconditiongt
true
false
ltexecutable statements 2gt
ltexecutable statements 1gt
ltstatement aftergt
9
if-else Flow Chart
avg midterm0.3 final0.7
avg midterm0.3 final0.7 if (avg lt 50)
cout ltlt Failed else cout ltlt Passed
cout ltlt this course ltlt endl
avg lt 50
true
false
cout ltlt Failed
cout ltlt Passed
cout ltlt this course ltlt endl
10
Quadratic Equation Example
  • Quadratic functions are of the form f(x) ax2
    bx c, where a, b, and c are constants.
  • We wish to create a program that finds the values
    of x where f(x) 0. These values of x are known
    as the roots of the equation.
  • The formula for finding the roots is
  • There are three cases that arise.

11
Quadratic Equation Example
  • Case 3 (b2 - 4ac) gt 0
  • In this case there are two roots for the
    quadratic equation
  • Example
  • 2x2 5x 3

Case 2 (b2 - 4ac) lt 0 In this case there are no
roots for the quadratic equation. Example 3x2
2x 4
Case 1 (b2 - 4ac) 0 In this case there is
only one root for the quadratic
equation. Example 3x2 6x 3
12
Quadratic Equation Example
  • The square root portion of the quadratic equation
    (b2 - 4ac) determines which of the three cases we
    have. This part of the equation is known as the
    discriminant.
  • Pseudo code
  • Read in values for a, b, and c
  • Compute the discriminant (b2 - 4ac)
  • If the discriminant is negative
  • Display There are no real solutions
  • else
  • if the discriminant is zero
  • compute root -b/2a
  • Display solution
  • else
  • compute root 1 (-b sqrt(discriminant))/
    (2a)
  • compute root 2 (-b - sqrt(discriminant))/
    (2a)
  • Display solutions
  • 4. End program

13
  • / header /
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • /
  • Read in values for a, b, and c
  • Compute the discriminant (b2 - 4ac)
  • If the discriminant is negative
  • Display There are no real solutions
  • else
  • if the discriminant is zero
  • compute root -b/2a
  • Display solution
  • else
  • compute root 1 (-b sqrt(discriminant))/
    (2a)
  • compute root 2 (-b - sqrt(discriminant))/
    (2a)
  • Display solutions

for sqrt() function
14
  • / header /
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • double a, b, c, discriminant, root1, root2
  • cout ltlt "Input a, b, and c for f(x) ax2
    bx c "
  • cin gtgt a gtgt b gtgt c
  • /
  • Compute the discriminant (b2 - 4ac)
  • If the discriminant is negative
  • Display There are no real solutions
  • else
  • if the discriminant is zero
  • compute root -b/2a
  • Display solution
  • else

15
  • / header /
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • double a, b, c, discriminant, root1, root2
  • // Read in values for a, b, and c
  • cout ltlt "Input a, b, and c for f(x) ax2
    bx c "
  • cin gtgt a gtgt b gtgt c
  • // Compute the discriminant (b2 - 4ac)
  • discriminant bb 4.0ac
  • /
  • If the discriminant is negative
  • Display There are no real solutions
  • else
  • if the discriminant is zero

16
  • / header /
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • double a, b, c, discriminant, root1, root2
  • // Read in values for a, b, and c
  • cout ltlt "Input a, b, and c for f(x) ax2
    bx c "
  • cin gtgt a gtgt b gtgt c
  • // Compute the discriminant (b2 - 4ac)
  • discriminant bb 4.0ac
  • if (discriminant lt 0)
  • // discriminant is negative
  • else
  • // discriminant is positive

17
  • / header /
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • double a, b, c, discriminant, root1, root2
  • // Read in values for a, b, and c
  • cout ltlt "Input a, b, and c for f(x) ax2
    bx c "
  • cin gtgt a gtgt b gtgt c
  • // Compute the discriminant (b2 - 4ac)
  • discriminant bb 4.0ac
  • if (discriminant lt 0)
  • // discriminant is negative
  • cout ltlt "\nThere are no real solutions ltlt
    endl
  • else

A second if else statement is nested within the
first
Discriminant already determined not to be
negative nor zero therefore it must be positive
18
Multiple-Alternative Conditionals
  • if (condition 1)
  • ltstatements 1gt
  • else if (condition 2)
  • ltstatements 2gt
  • else if (condition 3)
  • ltstatements 3gt
  • else
  • ltstatements 4gt
  • Only one of these statements will ever be
    evaluated. For statement 3 to execute, condition
    1 must be false, condition 2 must be false, and
    condition 3 must be true.
  • Note you can have as many else-if statements as
    you want.

19
Example Percentage to Letter Grade
  • Convert marks according to the following letter
    grades
  • gt 80 A
  • 70 79 B
  • 60 69 C
  • 50 59 D
  • lt 50 F

20
Example Percentage to Letter Grade
  • / header /
  • include ltiostream.hgt
  • void main(void)
  • double averageMark
  • char letterGrade
  • cout ltlt "Enter averageMark "
  • cin gtgt averageMark
  • if (averageMark gt 80.0)
  • letterGrade 'A'
  • else if (averageMark gt 70.0)
  • letterGrade 'B'
  • else if (averageMark gt 60.0)
  • letterGrade 'C'
  • else if (averageMark gt 50.0)
  • letterGrade 'D'

21
Multiple-Alternative Conditionals
averageMark gt 80
true
letterGrade 'A'
false
averageMark gt 70
true
letterGrade B'
if (averageMark gt 80.0) letterGrade
'A' else if (averageMark gt 70.0)
letterGrade 'B' else if (averageMark gt
60.0) letterGrade 'C' else if
(averageMark gt 50.0) letterGrade 'D'
else letterGrade 'F' cout ltlt "\n The
letter grade is " ltlt letterGrade ltlt endl
false
averageMark gt 60
true
letterGrade C'
false
averageMark gt 50
true
letterGrade D'
false
letterGrade F'
cout statement
22
Example Date Comparison
  • Compare two dates (year, month, day) and
    determine which date precedes the other.

/ header / include ltiostream.hgt void
main(void) int year1, month1, day1 int
year2, month2, day2 cout ltlt "Enter the first
date (year month day) " cin gtgt year1 gtgt month1
gtgt day1 cout ltlt "\nEnter the second date (year
month day) " cin gtgt year2 gtgt month2 gtgt day2
23
  • if (year1 lt year2)
  • cout ltlt "\nThe first date is before the
    second" ltlt endl
  • else if (year1 year2)
  • if (month1 lt month2 )
  • cout ltlt "\nThe first date is before the
    second" ltlt endl
  • else if (month1 month2)
  • if (day1 lt day2)
  • cout ltlt "\nThe first date is before the
    second" ltlt endl
  • else if (day1 day2)
  • cout ltlt "\nThe two dates are the same"
    ltlt endl
  • else
  • // year1 year2, month1 month2 and
    day1 gt day2
  • cout ltlt "\nThe second date is before
    the first" ltlt endl
  • else
  • // year1 year2 and month1 gt month2
  • cout ltlt "\nThe second date is before the
    first" ltlt endl
  • else

24
Problem 1 Temperature
Write a program that outputs the temperature
condition given a numerical temperature specified
in Celcius, according to the following
scale temperature lt 0 Below freezing temperatur
e 0 At the freezing point 0 lt temperature ?
15 Cold 15 lt temperature ? 30 Warm 30 lt
temperature Hot
  • Enter the temperature 13.5
  • The temperature is cold.

Enter the temperature 0 The temperature is at
the freezing point.
Write a Comment
User Comments (0)
About PowerShow.com