Chapter 2 Part 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

2.

0 BASIC PROGRAM ELEMENTS

2.3 Apply operators and


expression
Operator

 Once we know of the existence of variables


and constants, we can begin to operate with
them.
 For that purpose, C++ integrates operators.
 Operator is a symbol that represents a
specific action. For example, a plus sign (+)
is an operator that represents addition.
Operator

 Operators includes Arithmetic's Operators,


Assignment Operators, Increment and
Decrement Operators(Unary), Relational
Operators, Logical Operators and
Conditional Operators
Types of Operators

Arithmetic's Operators
 The five arithmetical operations supported
by the C++ language are:

+ addition
- subtraction
* multiplication
/ division
% modulus
Types of Operators

 Operations of addition, subtraction, multiplication


and division literally correspond with their respective
mathematical operators.
 The only one that you might not be so used to see is
modulus; whose operator is the percentage sign (%).
 Modulus is the operation that gives the remainder of
a division of two values. For example, if we write:
a = 11 % 3;

 the variable a will contain the value 2, since 2 is the


remainder from dividing 11 between 3.
Types of Operators

Assignment (=)
 The assignment operator assigns a value to a
variable.

a = 5;

 This statement assigns the integer value 5 to the


variable a.
 The part at the left of the assignment operator (=) is
known as the lvalue (left value) and the right one as
the rvalue (right value).
Types of Operators

 The lvalue has to be a variable whereas the rvalue


can be either a constant, a variable, the result of an
operation or any combination of these.

 The most important rule when assigning is the right-


to-left rule:
 The assignment operation always takes place from
right to left, and never the other way:

a = b;
Types of Operators

Compound assignment (+=, -=, *=, /=, %=)


 When we want to modify the value of a variable by
performing an operation on the value currently stored in
that variable we can use compound assignment
operators:
expression equivalent to
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
Types of Operators

// compound assignment operators Output::

#include <iostream.h> 5
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
return 0;
}
Types of Operators

Increment and decrement operator


 Shortening even more some expressions, the increase operator
(++) and the decrease operator (--) increase or reduce by one the
value stored in a variable.
 They are equivalent to +=1 and to -=1, respectively. Thus:

1. c++;
2. c+=1;
3. c=c+1;

 are all equivalent in its functionality: the three of them increase by


one the value of c.
Types of Operators

 A characteristic of this operator is that it can


be used both as a prefix and as a suffix.
 That means that it can be written either
before the variable identifier (++a) or after it
(a++).
Types of Operators

 Although in simple expressions like a++ or


++a both have exactly the same meaning, in
other expressions in which the result of the
increase or decrease operation is evaluated
as a value in an outer expression they may
have an important difference in their
meaning.
Types of Operators

 In the case that the increase operator is used


as a prefix (++a) the value is increased
before the result of the expression is
evaluated.
 In case that it is used as a suffix (a++) the
value stored in a is increased after being
evaluated.
Types of Operators

Example 1 Example 2

B=3; B=3;
A=++B; A=B++;
// A contains 4 // A contains 3
Types of Operators

Relational operators ( ==, !=, >, <, >=, <= )


 In order to evaluate a comparison between
two expressions we can use the relational
and equality operators.
 The result of a relational operation is a
Boolean value that can only be true or false,
according to its Boolean result.
Types of Operators

 We may want to compare two expressions, for example, to know if


they are equal or if one is greater than the other is.
 Here is a list of the relational and equality operators:
== Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to


Types of Operators

 Here there are some examples:

(7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
Types of Operators

 Of course, instead of using only numeric constants, we


can use any valid expression, including variables.
Suppose that a=2, b=3 and c=6,

(a == 5) // evaluates to false since a is not


equal to 5.
(a*b >= c) // evaluates to true since (2*3 >= 6)
is true.
(b+4 > a*c) // evaluates to false since (3+4 >
2*6) is false.
((b=2) == a) // evaluates to true.
Types of Operators

 Be careful! The operator = (one equal sign) is not the


same as the operator == (two equal signs), the first
one is an assignment operator (assigns the value at
its right to the variable at its left) and the other one
(==) is the equality operator that compares whether
both expressions in the two sides of it are equal to
each other.
 Thus, in the last expression ((b=2) == a), we first
assigned the value 2 to b and then we compared it to
a, that also stores the value 2, so the result of the
operation is true.
Types of Operators

Logical operators ( !, &&, || )


 The Operator ! is the C++ operator to
perform the Boolean operation NOT, it has
only one operand, located at its right, and the
only thing that it does is to inverse the value
of it, producing false if its operand is true and
true if its operand is false.
Types of Operators

 Basically, it returns the opposite Boolean value of


evaluating its operand. For example:

!(5 == 5) // evaluates to false because the


expression at its right (5 == 5)
is true.
!(6 <= 4) // evaluates to true because (6 <= 4)
would be false.
!true // evaluates to false
!false // evaluates to true.
Types of Operators

 The logical operators && and || are used


when evaluating two expressions to obtain a
single relational result.
 The operator && corresponds with Boolean
logical operation AND.
 This operation results true if both its two
operands are true, and false otherwise.
Types of Operators

 The following panel shows the result of operator &&


evaluating the expression a && b:

&& OPERATOR

a b a && b
true true true
true false false
false true false
false false false
Types of Operators

 The operator || corresponds with Boolean


logical operation OR.
 This operation results true if either one of its
two operands is true, thus being false only
when both operands are false themselves.
Types of Operators

 Here are the possible results of a || b:


|| OPERATOR

a b a || b
true true true
true false true
false true true
false false false
Types of Operators
 For example:

( (5 == 5) && (3 > 6) ) // evaluates to false (true && false ).

( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).


Types of Operators

Conditional operators
 ternary operator (working on three values)
 Format:
conditional Expression ? expression1 :expression2;
if the conditional Expression is true, expression 1
executes, otherwise if the conditional Expression is
false, expression 2 executes
Types of Operators

 Example
(a>b) ? (c=25) : (c=45);
 Program:
#include<iostream.h>
void main()
{
int a,b,c;
a=4;
b=5;
(a > b) ? (c=25) : (c=35);
cout<<a+c;
}
Typecasting

 Typecasting is making a variable of one type,


such as an int, act like another type, a char,
for one single operation.
 To typecast something, simply put the type of
variable you want the actual variable to act
as inside parentheses in front of the actual
variable.
 Example:
(char)a will make 'a' function as a char.
Typecasting

 Example:
cout<< char ( 65 ) ;
The (char) is a typecast, telling the computer to
interpret the 65 as a character, not as a
number. It is going to give the character output
of the equivalent of the number 65 (It should be
the letter A for ASCII).
Operators’ Precedence

 When writing complex expressions with


several operands, we may have some
doubts about which operand is evaluated first
and which later.
Operators’ Precedence

 For example, in this expression:


a=5+7%2

we may doubt if it really means:

a = 5 + (7 % 2) // with a result of 6, or
a = (5 + 7) % 2 // with a result of 0
Operators’ Precedence

 The correct answer is the first of the two


expressions, with a result of 6.
 There is an established order with the priority
of each operator, and not only the arithmetic
ones (those whose preference come from
mathematics) but for all the operators which
can appear in C++.
Operators’ Precedence

 From greatest to lowest priority, the priority order is as


follows:
Operator
! ++ --
* / %
From
greatest
+ -
to
< <= > >= lowest
== != priority
&&
||
=
Write expression using operator

 An expression in a programming language


is a combination of values, variables,
operators, and functions that are interpreted
(evaluated) according to the particular rule of
precedence and of association for a
particular programming language, which
computes and then produces (returns, in a
stateful environment) another value.
Write expression using operator

 Examples:
a. 25 + 18 * 2;
b. 3.00*40+1.5*6+10;
c. 2*payrate+bonus;
d. (4==4)
e. (grossPay > taxableAmount)
Apply expression and operators in
program

#include <iostream.h>
int main()
{
float payrate;
float hours;
float totalPay;

cout<<”Enter pay rate: RM “;


cin>>payrate;

cout<<”Enter hours worked: “;


cin>>hours;

totalPay=payrate*hours;
cout<<”\nTotal pay is RM “<<totalPay<<endl;
}

You might also like