Title: Selection Statements
1Selection Statements
2Example
- Write a program that computes the final grade of
a student based on their midterm and final exam,
which are worth 30 and 70, respectively. If the
student has failed the course (i.e. a final grade
less than 50) the program should display this.
3Flow Chart
- include ltiostreamgt
- using namespace std
- int main()
-
- double midterm, final, grade
-
- cout ltlt "Enter midterm and final exam mark
" - cin gtgt midterm gtgt final
-
- grade 0.3midterm 0.7final
-
- cout ltlt "Final grade is " ltlt grade ltlt "" ltlt
endl -
- if (grade lt 50.0)
- cout ltlt "Student has failed the course."
ltlt endl -
-
get midterm and final mark
grade 0.3midterm 0.7final
display grade
false
grade lt 50.0
true
display student has failed
end of program
4if Syntax
The textbooks states that if there is only one
executable statement, you do not need to include
the braces however, for this course just always
include them.
- 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.
5if Flow Chart
ltstatement beforegt
ltstatement beforegt if (ltconditiongt)
ltexecutable statementsgt ltstatement aftergt
false
ltconditiongt
true
ltexecutable statementsgt
ltstatement aftergt
6if Flow Chart
y x 5
y x 5 if (y gt 100) y 100 cout ltlt y
ltlt endl
false
y gt 100
true
y 100
cout ltlt y ltlt endl
7if/else Syntax
- if (ltconditiongt)
- ltexecutable statements 1gt
- else
- ltexecutable statements 2gt
-
- If the condition evaluates to true then the
executable statements 1 are executed (executable
statement 2 are ignored) otherwise, those
executable statements 2 are executed (executable
statement 1 are ignored).
8if-else Flow Chart
ltstatement beforegt
ltstatement beforegt if (ltconditiongt)
ltexecutable statements 1gt else
ltexecutable statements 2gt ltstatement
aftergt
true
false
ltconditiongt
ltexecutable statements 2gt
ltexecutable statements 1gt
ltstatement aftergt
9Flow Chart
get midterm and final mark
- include ltiostreamgt
- using namespace std
- int main()
-
- double midterm, final, grade
-
- cout ltlt "Enter midterm and final exam mark
" - cin gtgt midterm gtgt final
-
- grade 0.3midterm 0.7final
-
- cout ltlt "Final grade is " ltlt grade ltlt "" ltlt
endl -
- if (grade lt 50.0)
- cout ltlt "Student has failed the course."
ltlt endl - else
- cout ltlt "Student has passed the course."
ltlt endl
grade 0.3midterm 0.7final
display grade
true
false
grade lt 50.0
display student has failed
display student has passed
end of program
10Quadratic 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.
11Quadratic 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
12Flow Chart
get values for a, b, and c
compute discriminant bb - 4.0ac
true
false
discriminant lt 0.0
true
false
display there are no real solutions
discriminant 0.0
root1 (-b sqrt(discriminant))/(2.0a)
root1 (-b)/(2.0a)
root2 (-b - sqrt(discriminant))/(2.0a)
display single root
display two roots
end of program
13Quadratic 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
- 1. Read in values for a, b, and c
- 2. Compute the discriminant (b2 - 4ac)
- 3. 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
14- include ltiostreamgt
- include ltcmathgt
- using namespace std
- int main()
-
- 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" ltlt endl - cin gtgt a gtgt b gtgt c
-
- // Compute the discriminant (b2 - 4ac)
- discriminant bb - 4.0ac
- if (discriminant lt 0.0)
- // discriminant is negative
- cout ltlt "There are no real solutions" ltlt
endl
for sqrt() function
A second if else statement is nested within the
first
Discriminant already determined not to be
negative nor zero therefore it must be positive
15Multiple-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.
16Example 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
17Multiple-Alternative Conditionals
averageMark gt 80
true
letterGrade 'A'
false
averageMark gt 70
true
letterGrade B'
if (numGrade gt 80.0) letterGrade 'A'
else if (numGrade gt 70.0) letterGrade
'B' else if (numGrade gt 60.0) letterGrade
'C' else if (numGrade gt 50.0)
letterGrade 'D' else letterGrade
'F' cout ltlt "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
18Example Percentage to Letter Grade
- include ltiostreamgt
- using namespace std
- int main()
-
- double numGrade
- char letterGrade
-
- cout ltlt "Enter grade "
- cin gtgt numGrade
- if (numGrade gt 80.0)
- letterGrade 'A'
- else if (numGrade gt 70.0)
- letterGrade 'B'
- else if (numGrade gt 60.0)
- letterGrade 'C'
- else if (numGrade gt 50.0)
19Example Date Comparison
- Compare two dates (year, month, day) and
determine which date precedes the other.
include ltiostreamgt using namespace std int
main() 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 "Enter the second date (year
month day) " cin gtgt year2 gtgt month2 gtgt day2
20- if (year1 lt year2)
- cout ltlt "The first date is before the
second" ltlt endl - else if (year1 gt year2)
- cout ltlt "The second date is before the
first" ltlt endl - else
- // year1 is equal to year2
- if (month1 lt month2)
- cout ltlt "The first date is before the
second" ltlt endl - else if (month1 gt month2)
- cout ltlt "The second date is before the
first" ltlt endl - else
- // year1 is equal to year2 and month1
is equal to month2 - if (day1 lt day2)
- cout ltlt "The first date is before
the second" ltlt endl - else if (day1 gt day2)
- cout ltlt "The second date is before
the first" ltlt endl - else
- cout ltlt "The two dates are the same"
ltlt endl -