You are on page 1of 36

Relational, logical and increment

decrement operators
(operator hubungan, mantik dan peningkatan dan penurunan)

MARLINA MOHAMAD
BBP 15103
(COMPUTER PROGRAMMING)
WEEK 6.1
Learning outcomes

 At the end of this topic, you should be able to:


 Understand relational operators

 Use logical operators when necessary

 Manipulate decrement and increment operators efficiently


Relational operators

 Sometimes, in order to make decision, you must be


able to express conditions and make comparisons
 For example; the premium on an insurance policy is
determined by the age, health status of the policy
holder.
 For example, nonsmokers receive lower premiums
than smokers
 This example involves comparing items
Relational operators

 In C++, a condition is represented by a logical


(Boolean) expression
 Expressions containing relational operators always
evaluate to true or false
This is
relational
operator
i>j  This is a logical (Boolean) expression

Example:
If i=10, j=3, so the logical value is true
If i=2, j=5, so the logical value is false
Relational operators

 A relational operator allow you to make comparisons


in a program
 The following table shows the relational operator in
C++
 Notice that 4 of the C++ relational operators contain
2 symbols
 when entering these operators, be sure that you do not enter a
space between the symbols and be sure to enter both symbols
in the exact order
Relational operators

Table: Relational operators in C++


Relational operators

 Each of the relational operators is a binary operator;


that is, it requires two operands
Relational operators

Example
Relational operators

 For char values, whether an expression using


relational operators evaluates to true or false
depends on a machine’s collating sequence
 The collating sequence of some of the character is
show on the following ASCII table
ASCII Table
Relational operators

What are the logical values for the following logical


expressions?

‘R’ > ‘T’ is ??

‘+’ < ‘*’ is ??

‘A’ <= ‘a’ is ??


Relational operators

What are the logical values for the following logical


expressions?

‘R’ > ‘T’ is false

‘+’ < ‘*’ is false

‘A’ <= ‘a’ is true


Relational operators
Logical (Boolean) Operator

 Logical (Boolean) operators enable you to combine


two or more logical expression into compound
expression
 The following table show the list of two commonly
used logical operators
Logical (Boolean) Operator

Table: The logical operators


Operator Operation
And ( && ) All sub-expression must be true for the compound
condition to evaluate to true
Or ( || ) Only one of the sub-expression needs to be true for
the compound expression to evaluate to true
Not ( ! ) Not
Logical (Boolean) Operator

Example 1
int quantity = 0;
cin >> quantity;
if ( quantity > 0 && quantity < 50)

 The compound expression evaluates to true when


the number stored in the quantity variable is
greater than zero and at the same time , less than
50; otherwise, it evaluates to false
Logical (Boolean) Operator

Example 2
int umur = 0;
cin >> umur;
if ( age == 21 || age > 55 )

 The compound expression evaluates to true when


the number stored in the umur variable is either
equal to 21 or greater than 55; otherwise it evaluates
to false
Logical (Boolean) Operator

Example 3
int quantity = 0;
double price = 0.0;
cin >> quantity;
cin >> price;
if ( quantity < 100 && price < 10.35 )

 The compound expression evaluates to true when


the number stored in the quantity variable is less
than 100 and, at the same time, the number stored in
the price variable is less than 10.35, otherwise, it
evaluates to false
Logical (Boolean) Operator

Example 4
int quantity = 0;
double price = 0.0;
cin >> quantity;
cin >> price;
if ( quantity > 0 && quantity <100 || price >34.55)

 The compound expression evaluates to true when either


(or both) of the following is true
 The number stored in the quantity variable is between 0 and 100 or
 The number stored in the price variable is greater than 34.55;
otherwise it evaluates to false
 The && operator is evaluated before the || operator
because it has a higher precedence.
Not ( ! ) operator

Table: The Not (!) operator

Example Not (!)


And ( && ) operator
Table: The And (&&) operator

Example And ( && )


Or ( || ) operator
Table: The Or ( || ) operator
Or ( || ) operator

Example Or ( || )
Order or precedence

Table: Order of precedence


Example

Consider the following expression:


Expression Value/ explanation
!found false
Because found is true, !found is false
Hours > 40.00 True
Because hours is 45.30 and 45.30 >40.00 is true,
the expression hours > 40.00 evaluates to true
Example (cont)

Consider the following expression:


Expression Value/ explanation
!age=20 false
age is 20,which is nonzero, so age is true.
Therefore, !age=20 is false
!found && (age >=18) False
!found is false; age > 18 is 20>18 is true.
Therefore, !found && (age >=18) is false
&& true, which evaluates to false
Increment and Decrement operator

 Suppose count is an int variable. The statement:

 increment the value of count by 1.


 To expedite the execution of such statements, C++
provides the increment operator, ++, which
increases the value of variable by 1 and
 The decrement operator, -- which decreases the
value of a variable by 1
Increment and Decrement operator

 Increment and decrement operators have two forms,


pre and post
 The syntax of the increment operator is:

 The example :
Increment and Decrement operator

 The syntax of the decrement operator is:

 The example:
Increment and Decrement operator
Review questions

True or false?
 Suppose x =5. After the statement y=x++; executes,
y is 5 and x is 6.
Answer: True

 Suppose a= 5. After the statement ++a; executes,


the value of a is still 5 because the value of the
expression is not saved in another variable,
Answer: false
Review questions

 Typing cin> age; rather than cin>>age; is an


syntax
example of ___________ error.

 The .cpp file that contain your C++ instruction is


source
called the ___________ file.
a. Executable
b. Object
c. Source
d. statement
Review questions

 Rewrite the age=age+1; statement using an


age = age++ .
increment operator___________

 In a C++ program, the body of a function is enclosed


curly braces .
in ___________
a. curly braces
b. parentheses
c. Square bracket
d. None of the above
Review questions

 Which of the following determines whether an int


variable named quantity contains the number
100?
a. if (quantity = 100)
b. if quantity = 100
c. if (quantity == 100)
d. None of the above

Answer : if (quantity == 100)


Review questions

 Which of the following is the inequality operator in


C++?
a. &=
b. ≠
c. !=
d. None of the above

Answer : !=
Summary

 You have learned:


 Relational operators

 Logical operators

 Manipulate decrement and increment operators efficiently

You might also like