Relational Expressions - PowerPoint PPT Presentation

1 / 74
About This Presentation
Title:

Relational Expressions

Description:

using namespace std; int main() { // Begin Main Function. int i = 2; int j = 3; ... NOTE: In a conditional expression, you need to have a double equals sign ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 75
Provided by: jason102
Category:
Tags: an | expressions | have | relational | signs | std | that | you

less

Transcript and Presenter's Notes

Title: Relational Expressions


1
Chapter 5
  • Relational Expressions
  • May the computer gods have mercy

2
Decision 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)
4
Process
5
Process
Selectionor Decision
6
Representing 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.

7
Relational 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

8
Logical Operators
  • C provides 3 logical operators in case more
    than one comparison needs to be made at one time.
  • Operator Meaning
  • and
  • or
  • ! not

9
Example 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

12
Comment 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

14
Libraries
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

16
Main 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

18
Variables
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

20
cout 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

22
cout 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

24
The 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

26
Return 0
  • Ends the program

27
Example 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

30
New 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

32
The 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

34
Setting 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

36
Printing the the_bool variable
37
Combining 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)

38
Order 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

39
Example 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

42
The 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

44
The 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.

45
Introduction 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.

46
Using 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

47
Using 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.

48
Example of if/else
  • if (i 4)
  • cout ltlt "number is 4" ltlt endl
  • else
  • cout ltlt "number not 4" ltlt endl

49
Example 4
  • Logical Example
  • Using a flow chart

50
(No Transcript)
51
Run through the flow chart
52
(No Transcript)
53
Asking the user to enter in a number
54
Asking the user to enter in a number
The variable where the number will be stored
55
Asking the user to enter in a number
The variable where the number will be stored
The IF statement condition statement
56
Asking 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
57
Asking 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
58
Use 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

60
Use the flowchart to create the program
  • Step 2
  • Identify any variables

61
The variable where the number will be stored
62
Use 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

64
Use the flowchart to create the program
  • Step 3
  • Follow the flowchart

65
Asking 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

67
The 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

69
The 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)

71
What 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)

73
What 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)
Write a Comment
User Comments (0)
About PowerShow.com