Title: CS102 Introduction to Computer Programming
1CS102Introduction to Computer Programming
- Chapter 4 Making Decisions
2Chapter 4 Topics
- Relational Operators
- Value of a Relationship
- What is Truth
- The if Statement
- Flags
- Expanding the if Statement
- The if/else Statement
- The if/else if Statement
- Using the Trailing else
3Relational Operators
- Relational Operator Meaning
- gt greater than
- lt Less than
- gt greater than or equal to
- lt less than or equal to
- equivalent to
- ! not equal to
Concept - Relational operators allow you to
compare numeric values
4Value of a Relationship
- All expressions have a value
- The value of a relational expression is either 1
if true or 0 if false - A relational expression is also called a Boolean
expression - Think of a relational expression as a question
the computer will answer not a statement of fact
Concept - A relational expression is either true
or false
5Program 4-1
- / This program displays the values of true and
false states./ - include ltiostream.hgt
- void main(void)
-
- int True, False
- int X 5, Y 10
- True (X lt Y)
- False (Y X)
- cout ltlt "True is " ltlt True ltlt endl
- cout ltlt "False is " ltlt False ltlt endl
-
The computer represents the abstract concept of
truth as an integer value. 1 for true 0 for not
true
Program OutputTrue is 1False is 0
6What is Truth
- C deals with the abstract states of true or
false by converting them to numbers - Because a relational expression is an expression
and has a value, the value can be - assigned to a variable
- displayed with cout
- used in a mathematical expression
Concept - True and false are values that can be
stored in memory
7Check Point
- If x is 5, y is 6 and z is 8
- x 5
- 7 lt (x2)
- zgt4
- (2x)!y
- z!4
- xgt0
- xlt(y2)
- Correct or not
- (xlty) (ygtx)
- (x!y) (ygtx)
- (xgty) (yltx)
- xgty and xltz then yltz
- xgty and xz then yz
- x!y and x!z then z!y
T T T T T T T
F F T T F F
8The if Statement
- The if statement allows a program to have more
than one path of execution - if (expression)
- statement
- If the value of the expression is
- 0, then statement is skipped
- Not 0, then statement is executed
- Note the goes at the end of the if statement
not at the end of the line
Concept - The if statement can cause other
statements to execute only under certain
conditions
9The if Flow Chart Symbol
if expression
!0
statement
0
end
10Program 4-2
This program uses a simple if statement to test
the value of average
- // This program averages 3 test scores
- include ltiostream.hgt
- void main(void)
-
- int Score1, Score2, Score3
- float Average
- cout ltlt "Enter 3 test scores and I will average
them " - cin gtgt Score1 gtgt Score2 gtgt Score3
- Average (Score1 Score2 Score3) / 3.0
- cout.precision(1)
- cout.setf(iosshowpoint iosfixed)
- cout ltlt "Your average is " ltlt Average ltlt endl
- if (Average gt 95)
- cout ltlt "Congratulations! That's a high
score!\n" -
Program Output
Enter 3 test scores and I will average them 80
90 70 Enter Your average is 80.0
Enter 3 test scores and I will average them 100
100 100 Enter Your average is
100.0 Congratulations! That's a high score!
11The if Statement continued
- Be careful with comparing floating point numbers
- Rounding errors can cause unwanted results
- stick with lt or gt relationships
- Any non-zero value is considered true
- is not the same as
- x y is either 0 or 1
- if (x y )
- x y is the value of y
- if (x y )Unless y0 this condition is always
true
12Flags
- Flag variables are set by the programmer to
trigger a specific operation if a specific
condition has been met - if the flag 0 , then the condition is not met
- if the flag ! 0, then the condition has been met
- Note Variables are not automatically set to 0
- always be sure to initialize flags
Concept - A flag is a variable, usually an
integer, that signals when a condition has been
met
13Program 4-6
- / This program averages 3 test scores. It uses
the variable HighScore as a flag./ - include ltiostream.hgt
- void main(void)
-
- int Score1, Score2, Score3
- int true 1, false 0
- float Average
- int HighScore false
- cout ltlt "Enter your 3 test scores and I will
average them " - cin gtgt Score1 gtgt Score2 gtgt Score3
- Average (Score1 Score2 Score3) / 3.0
- if (Average gt 95)
- HighScore true
- // Set the flag variable
-
cout ltlt fixed ltlt showpoint ltltsetprecision(1)
cout ltlt "Your average is " ltlt Average ltlt
endl if (HighScore) cout ltlt "Congratulations!
That's a high score!\n"
Program Output with Example Input Enter your 3
test scores and I will average them 100 100 100
Enter Your average is 100.0 Congratulations!
That's a high score!
The conditional expression in an if statement
does not have to be a relational expression.
14Expanding the if Statement
- If you need to execute more than one statement
when the condition is true - start with an open bracket
-
- enter all the statements to be executed ending
each one with a - End with a closing bracket
-
Concept - The if statement can conditionally
execute a block of statements enclosed in brackets
15Program 4-7
- / This program averages 3 test scores. It uses
the variable HighScore as a flag./ - include ltiostream.hgt
- void main(void)
-
- int Score1, Score2, Score3
- float Average
- bool HighScore false
- cout ltlt "Enter 3 test scores and I will average
them " - cin gtgt Score1 gtgt Score2 gtgt Score3
- Average (Score1 Score2 Score3) / 3.0
- if (Average gt 95)
- HighScore true
- // Set the flag variable
- cout ltlt fixed ltlt showpoint
- ltlt setprecision(1)
- cout ltlt "Your average is " ltlt Average ltlt endl
- if (HighScore)
-
- cout ltlt "Congratulations!\n"
- cout ltlt "That's a high score.\n"
- cout ltlt "You deserve a pat on the back!\n"
-
-
Program Output Enter your 3 test scores and I
will average them 100 100 100 Enter Your
average is 100.0 Congratulations! That's a high
score! Your deserve a pat on the back
If the condition is true everything inside the
braces is executed
16Programming Style and the if Statement
- The conditionally executed statement should
appear on the line after the if statement. - The conditionally executed statement should be
indented one level from the if statement. - Note Each time you press the tab key, you are
indenting one level.
17The if/else Statement
- The if/else statement selects one of two
statements or blocks of code to execute - if (expression)
- statement or block
- else
- statement or block
Concept - The if/else statement will execute one
group of statements if the expression is true and
another if it is false
18The if/else Flow Chart Symbol
if expression
True
Statement or block
False
Statement or block
end
19Program 4-9
- / This program uses the modulus operator to
determine if a number is odd or even. If evenly
divided the number is by 2, it is an even number.
A remainder indicates it is odd./ - include ltiostream.hgt
- void main(void)
-
- int Number
- cout ltlt "Enter an integer and I will tell you if
it\n" - cout ltlt "is odd or even. "
- cin gtgt Number
- if (Number 2 0)
- cout ltlt Number ltlt " is even.\n"
- else
- cout ltlt Number ltlt " is odd.\n"
-
The if/else allows the control of two paths with
one statement
Program Output Enter an integer and I will
tell you if it is odd or even. 17 Enter 17 is
odd.
20Program 4-10
- / This program asks the user for two numbers,
Num1 and Num2. Num1 is divided by Num2 and the
result is displayed. Before the division
operation, however, Num2 is tested for the value
0. If it contains 0, the division does not take
place./ - include ltiostream.hgt
- void main(void)
-
- float Num1, Num2, Quotient
- cout ltlt "Enter a number "
- cin gtgt Num1
- cout ltlt "Enter another number "
- cin gtgt Num2
- if (Num2 0)
-
- cout ltlt "Division by zero is not possible.\n"
- cout ltlt "Please run the program again and
enter\n" - cout ltlt "a number besides zero.\n"
-
- else
-
- Quotient Num1 / Num2
- cout ltlt "The quotient of " ltlt Num1 ltlt "
divided by " - cout ltlt Num2 ltlt " is " ltlt Quotient ltlt ".\n"
-
-
This program uses the if statement to avoid an
undesirable operation
21Program Output
- (When the user enters 0 for Num2)
- Enter a number 10 Enter
- Enter another number 0 Enter
- Division by zero is not possible.
- Please run the program again and enter
- a number besides zero.
22The if/else if Statement
- The tests are performed one after another until
one is found to be true. - The if/else statement selects one of two
statements or blocks of code to execute - if (expression)
- statement or block
- else if (expression)
- statement or block
Concept - The if/else if statement is a chain of
if statements
23Flow Chart for the if/else if
if expression
True
Statement or block
False
Else if expression
True
Statement or block
False
end
24Program 4-11
- / This program uses an if/else if statement to
assign a letter grade (A, B, C, D, or F) to a
numeric test score./ - include ltiostream.hgt
- void main(void)
-
- int TestScore
- char Grade
- cout ltlt "Enter your numeric test score and I
will\n" - cout ltlt "tell you the letter grade you earned
" - cin gtgt TestScore
- if (TestScore lt 60)
- Grade 'F'
- else if (TestScore lt 70)
- Grade 'D'
- else if (TestScore lt 80)
- Grade 'C'
- else if (TestScore lt 90)
- Grade 'B'
- else if (TestScore lt 100)
- Grade 'A'
- cout ltlt "Your grade is " ltlt Grade ltlt
".\n" -
Program Output Enter your test score and I will
tell you the letter grade you earned 88
Enter Your grade is B.
As soon as a true condition is met the if/else if
statement terminates
25Program 4-12
- // This program uses independent if statements to
assign a letter grade (A, B, C, D, or F) to a
numeric test score. - include ltiostream.hgt
- void main(void)
-
- int TestScore
- char Grade
- cout ltlt "Enter your test score and I will tell
you\n" - cout ltlt "the letter grade you earned "
- cin gtgt TestScore
- if (TestScore lt 60)
- Grade 'F'
- if (TestScore lt 70)
- Grade 'D'
- if (TestScore lt 80)
- Grade 'C'
- if (TestScore lt 90)
- Grade 'B'
- if (TestScore lt 100)
- Grade 'A'
- cout ltlt "Your grade is " ltlt Grade ltlt ".\n"
-
Program Output Enter your test score and I will
tell you the letter grade you earned 40
Enter Your grade is A.
Do you think it will work?
26Using the Trailing else
- Can be used to to take care of any case not
considered by the if expressions - if (expression)
- statement or block
- else if (expression)
- statement or block
- else
- statement or block
Concept - The trailing else placed at the end of
an if/else if statement provides default action
when none of the ifs are true
27Program 4-13
- / This program uses an if/else if statement to
assign a letter grade (A, B, C, D, or F) to a
numeric test score. A trailing else has been
added to catch test scores gt 100./ - include ltiostream.hgt
- void main(void)
-
- int TestScore
- cout ltlt "Enter your test score and I will tell
you\n" - cout ltlt "the letter grade you earned "
- cin gtgt TestScore
- if (TestScore lt 60)
-
- cout ltlt "Your grade is F.\n"
- cout ltlt "This is a failing grade. Better see
your " - cout ltlt "instructor.\n"
-
- else if (TestScore lt 70)
-
- cout ltlt "Your grade is D.\n"
- cout ltlt "This is below average. You should get
" - cout ltlt "tutoring.\n"
-
28Program continues
- else if (TestScore lt 80)
-
- cout ltlt "Your grade is C.\n"
- cout ltlt "This is average.\n"
-
- else if (TestScore lt 90)
-
- cout ltlt "Your grade is B.\n"
- cout ltlt "This is an above average grade.\n"
-
- else if (TestScore lt 100)
-
- cout ltlt "Your grade is A.\n"
- cout ltlt "This is a superior grade. Good
work!\n" -
- else
-
else cout ltlt TestScore ltlt " is an invalid
score.\n" cout ltlt "Please enter scores no
greatr than 100.\n"
Program Output Enter your test score and I will
tell you the letter grade you earned 104
Enter 104 is an invalid score. Please enter
scores no greater than 100.