chap 4 c++
TRANSCRIPT
Chapter 4
Arithmetic and Logical Expressions
BTE2313 Computer Programming
Lecturer: Samson Mekbib
LEARNING OUTCOME
Learn about arithmetic operators to preform calculations in C++
Explain how to solve and create a logical expression
Explain how to use operators AND ( && ) and OR ( l l )
LEARNING OUTCOME
Learn about arithmetic operators to preform calculations in C++
Explain how to solve and create a logical expression
Explain how to use operators AND ( && ) and OR ( l l )
Operator shortcuts
Example: Operator shortcuts
Logical relationship
To make decisions, you must be able to express conditions and make comparisons
Stating conditions and making comparisons
In C++ a condition is represented by a logical (Boolean) expression
True and False are logical (Boolean) values
RELATIONAL OPERATORS: A relational operator allows you to make comparison in a program
Relational operators:
Allow comparison
Require two operands
Evaluate to True or False
Relational operators in C++
N.B. : In C++, the symbol ==, which consists of two equal signs, is called the equivalence operator and determines whether two expressions are equal, whereas the assignment operator =, assigns the value of an expression to a variable.
Operator Description Example True when Value of num is
False when value of num is
== Equal to num == 10 10 Other than 10
!= Not equal to num != 10 Other than 10 10
> Bigger than num > 10 Bigger than 10 10 or smaller
>= Bigger than or equal num >= 10 10 or bigger Smaller than 10
< Less than num < 10 Smaller than 10 10 or bigger
<= Less than or equal num <= 10 10 or smaller Bigger than 10
! Not !(num==10) Other than 10 10
Relational operators and simple data types
You can use the relational operators with all the simple data types
Examples with integers and real numbers
Expression Meaning Value
8 < 15 8 is less than 15 true
6 != 6 6 is not equal to 6 false
2.5 > 5.8 2.5 is greater than 5.8 false
5.9 <= 7.5 5.9 is less than or equal to 7.5 true
Example: Equality of floating point numbers
Boolean Algebra
Or truth table
P Q P or Q
False False False
False True True
True False True
True True True
Boolean Algebra
Not truth table
P not P
False True
True False
Boolean Algebra
Can create complex logical expressions by combining simple logical expressions
Example
not (P and Q)
A truth table can be used to determine when a logical expression is true
P Q P and Q not (P and Q)
False False False True
False True False True
True False False True
True True True False
A Boolean Type C++ contains a data type named bool
Type bool data type has two symbolic constants
true
false
Examples
bool P = true;
bool Q = false;
bool R = true;
bool S = (P && Q);
The insertion and extraction operators could be used with bool types
In the output bool objects are displayed in binary notations (0 or 1)
Example:
cout << P << endl;
will print out either 1 or 0, depending on whether P is true or false respectively
A Boolean Type
Example: Suppose the following bool type objects are defined
bool P = true;
bool Q = false;
bool R = true;
bool S = false;
Then,
P is true Q
P && R P && S
P l l Q Q l l S
!S !R
true false
Logical (Boolean) Operators
Examples of logical expressions
Operator Precedence Revisited
Level Operator Associativity Type
1 () Left to right Parentheses
2 +, -, ++, --, ! Right to left Unary
3 *, /, % Left to right Multiplicative
4 +, - Left to right Additive
5 <<, >> Left to right Insertion/extraction
6 <, <=, >, >= Left to right Relational
7 ==, != Left to right Equality
8 && Left to right Logical AND
9 l l Right to left Logical OR
Operator Precedence Revisited
Example: Consider
5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
Is equivalent to:
((((5 *15) + 4) == 13) && (12 < 19))
||
((!false) == (5 < 24))
N.B. This is just an example and you will not be
using such complicated statements in your
programs.
Introductions on next chapter Conditional / Selection Structure
Examples: Simple conditional statements
if (score is greater than or equal to 90)
grate is A
if (hours worked are less than or equal to 40)
wages = rate * hours
Otherwise
wages = (rate * 40) + 1.5 * (rate * (hours -40))
if (temperature is greater than 20 degrees and it is not raining)
go to play golf!
Conditional Constructs in C++
Provide
Ability to control whether a statement list is executed
Two constructs
If statement
if
if-else
if-else-if
The Basic If Statement
Syntax
if (Expression)
Action
If the Expression is true then execute Action
Action is either a single statement or a group of statements within braces
Expression
Action
true false
Example
if (Value < 0) {
Value = -Value;
}
Value < 0
Value = -Value
true f alse
Is our number negative?
If Value is not less
than zero then our
number is fine as is
If Value is less than
zero then we need to
update its value to
that of its additive
inverse
Our number is
now definitely
nonnegative
Sorting Two Numbers
cout << "Enter two integers: ";
int Value1;
int Value2;
cin >> Value1 >> Value2;
if (Value1 > Value2) {
int RememberValue1 = Value1;
Value1 = Value2;
Value2 = RememberValue1;
}
cout << "The input in sorted order: "
<< Value1 << " " << Value2 << endl;
Semantics
value2 < value1
int rememberValue1 = value1
value1 = value2
value2 = rememberValue1
true f alse
Are the numbers
out of order
Rearrange value1
and value2 to
put their values
in the proper
order
The numbers were
initially in order
The numbers were
rearranged into the
proper order
The numbers are in
order
What is the Output?
int m = 5;
int n = 10;
if (m < n)
++m;
++n;
cout << " m = " << m << " n = " n << endl;
The If-Else Statement Syntax
if (Expression)
Action1
else
Action2
If Expression is true then execute Action1 otherwise execute Action2
if (v == 0) {
cout << "v is 0";
}
else {
cout << "v is not 0";
}
Expression
Action1 Action2
true false
Finding the Max
cout << "Enter two integers: ";
int Value1;
int Value2;
cin >> Value1 >> Value2;
int Max;
if (Value1 < Value2) {
Max = Value2;
}
else {
Max = Value1;
}
cout << "Maximum of inputs is: " << Max << endl;
Finding the Max
Value1 < Value2
Max = Value2 Max = Value1
true f alse
Is Value2 larger than Value1
Yes, it is . So Value2 is
larger than Value1. In
this case, Max is set
to Value2
No, its not. So Value1
is at least as large as
Value2. In this case,
Max is set to Value1
Either case, Max is set
correctly
Selection
It is often the case that depending upon the value of an expression we want to perform a particular action
Two major ways of accomplishing this choice
if-else-if statement
if-else statements “glued” together
Switch statement
An advanced construct
An If-Else-If Statement
if ( nbr < 0 )
{
cout << nbr << " is negative" << endl;
}
else if ( nbr > 0 )
{
cout << nbr << " is positive" << endl;
}
else
{
cout << nbr << " is zero" << endl;
}