Notes For C++

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

C++ Operators & Expressions

17th July, 2024

ICT172: Principles to Programming


Lecture Notes 1
Operators
• Operators are symbols for connecting data to perform
various mathematical operations.
• They tell the computer how to process the data.
• They can be classified as:
• Arithmetic operators
• Assignment operators
• Unary operators
• Relational operators
• Logical operators
ICT172: Principles to Programming
Lecture Notes
Arithmetic Operators
• The Arithmetic Operators Used in C++ language are;

• + (addition)
• - (subtraction or negation)
• * (multiplication)
• /(division)
• % (modulus)

ICT172: Principles to Programming


Lecture Notes 3
Addition (+)
• Adds two operands.

• Example: int sum = a + b;

• Example: int sum = 5+2;


• sum is 7

ICT172: Principles to Programming


Lecture Notes 4
Subtraction (-)

• Subtracts the second operand from the first.

• Example: int difference = a - b;

• Example: int difference = 5-2;


• difference is 3

ICT172: Principles to Programming


Lecture Notes 5
Multiplication (*)

• Multiplies two operands.

• Example: int product = a * b;

• Example: int product = 5*2;


• product is 10

ICT172: Principles to Programming


Lecture Notes 6
Division (/)
• Divides the numerator by the denominator.
• Example: int quotient = a / b;
• Note: If both operands are integers, this performs
integer division and any fractional part is discarded.
• Example: int quotient = 5 / 2;
• quotient is 2
• Example: int quotient = 5.0 / 2;
• quotient is 2.5

ICT172: Principles to Programming


Lecture Notes 7
Modulus (%)
• Returns the remainder of an integer division.

• Example: int remainder = a % b;

• Example: int remainder = 10%4;

• remainder is 2

ICT172: Principles to Programming


Lecture Notes 8
Unary vs Binary Operators
• Unary operator: An operator that has only one operand.

• Binary operator: An operator that has two operands.

- and + are both unary and binary arithmetic operators.

*, /, and % are binary and so must have two operands.

ICT172: Principles to Programming


Lecture Notes 9
Unary Operators
• The unary operators operate on one operand.

• The increment and decrement operators.


Operator Description Example Explanation
++ An increment operator x++ Equivalent to
increases the value of x=x+1
the operand by one
-- The decrement x-- Equivalent to
operator decreases the x=x-1
value of the operand
by one
ICT172: Principles to Programming
Lecture Notes
Increment (++)
• Increases the value of the operand by 1.
• Can be used in prefix (++a) or postfix (a++) form.

• Example:

• ++a; or a++;

ICT172: Principles to Programming


Lecture Notes
Increment (++)
#include <iostream>
using namespace std;
int main (){
int a = 10;
++a;
cout << "After prefix increment: " << a << endl;
a++;
cout << "After postfix increment: " << a << endl;
}

ICT172: Principles to Programming


Lecture Notes
Decrement Operator (--)
• Decreases the value of the operand by 1.

• Can be used in prefix (--b) or postfix (b--) form.

• Example: --b; or b--;

ICT172: Principles to Programming


Lecture Notes
Decrement (--)
#include <iostream>
using namespace std;
int main (){
int b = 2;
--b;
cout << "After prefix decrement: " << b << endl;
b--;
cout << "After postfix decrement: " << b << endl;
}

ICT172: Principles to Programming


Lecture Notes
Arithmetic Expressions
• An arithmetic expression is constructed by using
arithmetic operators and numbers.

• The numbers appearing in the expression are called


operands.

• The numbers that are used to evaluate an operator


are called the operands for that operator.

ICT172: Principles to Programming


Lecture Notes 15
Arithmetic Expressions
The following are some arithmetic expressions:
i. -5 Unary operator: An operator that has only one
ii. 8 - 7 operand.
iii. 3 + 4
Binary operator: An operator that has two operands.
iv. 2 + 3 *5
v. 5.6 + 6.2 *3
vi. x + 2 *5 + 6 / y
- and + are both unary and binary arithmetic operators.

However, as arithmetic operators, *, /, and % are binary


and so must have two operands.
ICT172: Principles to Programming
Lecture Notes 16
Basic C++ Operators

Symbol Operation Example Value


+ Addition 3+5 8
- Subtraction 43 - 25 18
* Multiplication 4*7 28
/ Division 9/2 4
% Modulus 20 % 6 2

ICT172: Principles to Programming


Lecture Notes
Example
#include <iostream> difference << endl;
using namespace std; int product = a * b;
cout << "Product: " << product
int main() { << endl;
int a = 10; int quotient = a / b;
int b = 3; cout << "Quotient: " << quotient
<< endl; int remainder = a % b;
int sum = a + b;
cout << "Remainder: " <<
cout << "Sum: " << sum <<
remainder << endl;
endl;
int difference = a - b;
cout << "Difference: " <<
ICT172: Principles to Programming
Lecture Notes
Operator Precedence
• When more than one arithmetic operator is used in an
expression, C++ uses the operator precedence rules to
evaluate the expression.

• According to the order of precedence rules for arithmetic


operators,

• *, /, % are at a higher level of precedence than +, -

• Note that the operators *, /, and % have the same level of


precedence. Similarly, the operators + and - have the same
level of precedence.

ICT172: Principles to Programming


Lecture Notes 19
Order of Precedence
• Order of precedence for some C++ operators is as
below:

ICT172: Principles to Programming


Lecture Notes 20
Operator Precedence
• For example, using the order of precedence rules,
evaluate the following:

3*7-6+2*5/4+6

23

ICT172: Principles to Programming


Lecture Notes 21
Assignment operators
• In C++, = is called the assignment operator.

• The assignment statement takes the following form:


• variable = expression;
• In an assignment statement, the value of the expression should
match the data type of the variable.
• The expression on the right side is evaluated, and its value is
assigned to the variable (and thus to a memory location) on
the left side.
• A variable is said to be initialized the first time a value is placed
in the variable.

ICT172: Principles to Programming


Lecture Notes 22
Shorthand Arithmetic Assignment Statements
• Variable assignments can take the form such as
number = number + 1;

• Since it is often the case that variables are assigned a


new value in function of their old value, C++ provides
a shorthand notation.

• Any of the operators "+" (addition), "-" (subtraction), "*"


(multiplication), "/" (division) and "%" (modulus) can be
prefixed to the assignment operator (=), as in the
following examples

ICT172: Principles to Programming


Lecture Notes
Shorthand Arithmetic Assignment Statements

Example: Equivalent to:


number += 1; number = number + 1;
total -= discount; total = total - discount;
bonus *= 2; bonus = bonus * 2;
time /= rush_factor; time = time / rush_factor;
change %= 100; change = change % 100;
amount *= count1 + count2; amount = amount * (count1 +
count2);

ICT172: Principles to Programming


Lecture Notes
Relational Operators
• A relational operator allows you to make comparisons between
values.
• C++ includes six relational operators that allow you to state
conditions and make Comparisons. Table lists the relational
operators.
Each of the relational operators is
a binary operator; that is, it
requires two operands.
Because the result of a
comparison is true or false,
expressions using these operators
evaluate to true or false.

ICT172: Principles to Programming


Lecture Notes 25
Basic C++ Relational Operators
Symbol Meaning Example Value
< less than 3<5 TRUE
<= less than or equal to 43 <= 25 FALSE
> greater than 4>7 FALSE
>= greater than or equal to 9 >= 2 TRUE
== equal to 20 == 6 FALSE
!= not equal to 20 != 6 TRUE

ICT172: Principles to Programming


Lecture Notes
Logical Operators
• Logical (Boolean) operators enable you to combine logical
expressions. C++ has three logical (Boolean) operators, as
shown below;

• Logical operators take only logical values as operands and


yield only logical values as results. The operator ! is unary, so it
has only one operand. The operators && and || are binary
operators.

ICT172: Principles to Programming


Lecture Notes 27
Boolean Expressions and Operators
Expression: True or False:

(6 <= 6) && (5 < 3) false


(6 <= 6) || (5 < 3) true
(5 != 6) true
(5 < 3) && (6 <= 6) || (5 != 6) true
(5 < 3) ||((6 <= 6) || (5 != 6))
!((5 < 3) && ((6 <= 6) || (5 != 6))) true

ICT172: Principles to Programming


Lecture Notes
Examples
• Consider the code snippet below. What is the value
returned by each of the conditions?
int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) =>
if( i != j || k < j) =>
if( j<= k || i > k) =>
if( j == k && m) =>
if(i) =>
if(m || j && i ) =>

ICT172: Principles to Programming


Lecture Notes 29
Answers
int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) => false
if( i != j || k < j) => true
if( j<= k || i > k) => true
if( j == k && m) => false
if(i) => true
if(m || j && i ) => true

Can you see why?

ICT172: Principles to Programming


Lecture Notes 30
C++ Expressions
• An expression is a statement that combines variables,
constants, operators and function call.

• It can be arithmetic, logical and relational for example:


• int z= x + y // arithmetic expression
• a>b // relational
• a==b // logical
• func(a, b) // function call

ICT172: Principles to Programming


Lecture Notes 31
C++ Expressions
• Arithmetic expressions are quite known.
• If all operands (that is, numbers) in an expression are
integers, the expression is called an integral
expression.

• If all operands in an expression are floating-point


numbers, the expression is called a floating-point or
decimal expression.

• An integral expression yields an integral result;


• A floating-point expression yields a floating-point
result.
ICT172: Principles to Programming
Lecture Notes 32
Mixed Expression
• An expression that has operands of different data
types is called a mixed expression.

• A mixed expression contains both integers and


floating-point numbers.

• The following expressions are examples of mixed


expressions:
• 2 + 3.5
• 6 / 4 + 3.9
• 5.4 * 2 - 13.6 + 18 / 2
ICT172: Principles to Programming
Lecture Notes 33
How C++ Evaluates Mixed Expressions
• If the operator has the same types of operands (that is, either
both integers or both floating-point numbers), the operator is
evaluated according to the type of the operands. Integer
operands thus yield an integer result; floating-point numbers
yield a floating-point number.

• If the operator has both types of operands (that is, one is an


integer and the other is a floating-point number), then during
calculation, the integer is changed to a floating-point number
with the decimal part of zero and the operator is evaluated.
The result is a floating point number.

ICT172: Principles to Programming


Lecture Notes 34
Example

ICT172: Principles to Programming


Lecture Notes 35
Type Conversion (Casting)
• The process of converting one (Data) type into a
different type is referred to as type conversion.
• Implicit type coercion
• This changes the value of one data type
automatically to another data type.
• Explicit type conversion
• This requires using the cast operator.
• The cast operator, also called type conversion or type
casting, takes the following form:
static_cast<dataTypeName>(expression)
ICT172: Principles to Programming
Lecture Notes
Examples

ICT172: Principles to Programming


Lecture Notes 37
Quiz
What value is returned by the following print statement?

cout << static_cast<int>(7.8 + static_cast<double>(15.5 / 2);

ICT172: Principles to Programming


Lecture Notes 38
Programming Practice Session

ICT172: Principles to Programming


Lecture Notes 39
End of Lecture 4

ICT172: Principles to Programming


Lecture Notes

You might also like