Title: Relational Expressions
1Chapter 5
- Relational Expressions
- May the computer gods have mercy
2Decision Making in Programs
- Almost all programs have branching structures
which require decision making. - A flowchart maps the decisions a program is to
make and the path down which each decision leads.
- A flowchart uses different shapes to show
different actions
3(No Transcript)
4Process
5Process
Selectionor Decision
6Representing True and False in C
- Computers make complex branching decisions by
making simple comparisons. - If a comparison is TRUE, C returns a 1 to
represent true. - If a comparison is FALSE, C returns a 0 to
represent false.
7Relational Operators
- C provides the following relational operators
for making comparisons. - Operator Meaning
- equal to
- gt greater than
- lt less than
- gt greater than or equal to
- lt less than or equal to
- ! not equal to
8Logical Operators
- C provides 3 logical operators in case more
than one comparison needs to be made at one time. - Operator Meaning
- and
- or
- ! not
9Example I
- Example 1 covers logical operators
10- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
11- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
12Comment Lines
13- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
14Libraries
15- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
16Main Function
17- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
18Variables
19- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
20cout line 1
- cout ltlt (i 2) ltlt endl
- (i 2)
- This is a conditional expression.
- It is asking if i is equal to 2.
- NOTE In a conditional expression, you need to
have a double equals sign - If you have the following expression (i 2) this
will set the variable i to the number 2 - This line will print the number 1. This means
the expression is True because i is equal to 2.
21- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
22cout line 2
- cout ltlt (i 1) ltlt endl
- (i 1)
- This line will print the number 0. This means
the expression is False because i does not equal
to 1.
23- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
24The other cout line
- cout ltlt (j gt i) ltlt endl
- This line will print the number 1.
- cout ltlt (j lt 3) ltlt endl
- This line will print the number 1.
- cout ltlt (j ! i) ltlt endl
- This line will print the number 1. ! means not
equal.
25- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
- cout ltlt (j gt i) ltlt endl
- cout ltlt (j lt 3) ltlt endl
- cout ltlt (j ! i) ltlt endl
26Return 0
27Example 2
- Bool data type
- Bool data type only store two possible values
- 1 for True
- 0 for False
- Variable
- We will expand on Example 1
28- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- bool the_bool
- the_bool (j i)
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
29- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- bool the_bool
- the_bool (j i)
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
30New Lines
31- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- bool the_bool
- the_bool (j i)
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
32The bool variable
- bool the_bool
- The data type is bool
- Remember only stores true or false
- The name of the variable is the_bool
33- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- bool the_bool
- the_bool (j i)
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
34Setting the variable
- the_bool (j i)
- (j i)
- Does j I
- NoIts false
- the_bool will equal 0
35- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- bool the_bool
- the_bool (j i)
- cout ltlt (i 2) ltlt endl
- cout ltlt (i 1) ltlt endl
36Printing the the_bool variable
37Combining More than Two Comparisons
- You can use logical operators to combine more
than two comparisons. - ok_to_ride (height_in_inches gt 45
- !back_trouble
- !heart_trouble)
38Order of Logical Operations
- You can mix logical operators in statements as
long as you understand the order in which the
logical operators will be applied. - The not operator (!) is applied first,
- ! means not
- Then the and operator ()
- means and
- Finally the or operator ()
- (shift \ key) means or
39Example 3
- Combining More than Two Comparisons
- Again, we will expand on Example 1
40- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (j 2) ltlt endl
- cout ltlt ((i 2) (j 2)) ltlt endl
- cout ltlt ((i 2) (j 2)) ltlt endl
41- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (j 2) ltlt endl
- cout ltlt ((i 2) (j 2)) ltlt endl
- cout ltlt ((i 2) (j 2)) ltlt endl
42The and Evaluation
- In an expression like
- in_range (i gt 0 i lt 11)
- If the first part of the comparison is false,
there is no need to check the second half so the
program will skip it. - If either i gt 0 is false or i lt 11 is false, the
whole comparison would be false.
43- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- int i 2
- int j 3
- cout ltlt (i 2) ltlt endl
- cout ltlt (j 2) ltlt endl
- cout ltlt ((i 2) (j 2)) ltlt endl
- cout ltlt ((i 2) (j 2)) ltlt endl
44The or Evaluation
- In an expression like
- in_range (i gt 0 i lt 11)
- If either part of the comparison is true, the who
comparison is true.
45Introduction to Selection Structures
- Sequence structures execute statements one after
another without changing the flow of the program.
- The structures that make decisions in C
programs are called selection structures. - When a decision is made in a program, a selection
structure controls the flow of the program based
on the decision.
46Using if
- The if structure is in most if not all
programming languages. - The expression that makes the decision is called
the control expression. - Example
- if (i 3)
-
- cout ltlt "The value of i is 3 ltlt endl
-
47Using if/else
- The if/else structure is sometimes called a
two-way selection structure. - Using if/else, one block of code is executed if
the control expression is true and another block
is executed if the control expression is false.
48Example of if/else
- if (i 4)
-
- cout ltlt "number is 4" ltlt endl
-
- else
-
- cout ltlt "number not 4" ltlt endl
-
49Example 4
- Logical Example
- Using a flow chart
50(No Transcript)
51Run through the flow chart
52(No Transcript)
53Asking the user to enter in a number
54Asking the user to enter in a number
The variable where the number will be stored
55Asking the user to enter in a number
The variable where the number will be stored
The IF statement condition statement
56Asking the user to enter in a number
What will be processed if the condition is true
The variable where the number will be stored
The IF statement condition statement
57Asking the user to enter in a number
What will be processed if the condition is True
The variable where the number will be stored
The IF statement condition statement
What will be processed if the condition is False
58Use the flowchart to create the program
- Step 1
- Setup a basic program with the 3 components
59- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- return 0
- // End Main Function
60Use the flowchart to create the program
- Step 2
- Identify any variables
61The variable where the number will be stored
62Use the flowchart to create the program
- Step 2
- Identify any variables
- number is going to be an integer (no decimal
places)
63- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- // Varibles
- int number 0
- return 0
- // End Main Function
64Use the flowchart to create the program
- Step 3
- Follow the flowchart
65Asking the user to enter in a number
66- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- // Varibles
- int number 0
- // input
- cout ltlt "Enter in a number between 1 and 50 "
-
- return 0
- // End Main Function
67The variable where the number will be stored
68- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- // Varibles
- int number 0
- // input
- cout ltlt "Enter in a number between 1 and 50 "
- cin gtgt number
- return 0
- // End Main Function
69The IF statement condition statement
70- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- // Varibles
- int number 0
- // input
- cout ltlt "Enter in a number between 1 and 50 "
- cin gtgt number
- // If statement
- if (number gt 1 number lt 50)
71What will be processed if the condition is True
72- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- // Varibles
- int number 0
- // input
- cout ltlt "Enter in a number between 1 and 50 "
- cin gtgt number
- // If statement
- if (number gt 1 number lt 50)
73What will be processed if the condition is False
74- // program1.cpp
- // Jason Long
- // Class Example
- include ltiostreamgt
- using namespace std
- int main()
- // Begin Main Function
-
- // Varibles
- int number 0
- // input
- cout ltlt "Enter in a number between 1 and 50 "
- cin gtgt number
- // If statement
- if (number gt 1 number lt 50)