You are on page 1of 13

Lecture 5

By Iqra Tahir
Operands and Operator

Operands Operator
• The numbers appearing in the • The numbers that are used to
expressions are called operands. evaluate an operator are called
The numbers that are used to the operands for that operator
evaluate an operator are called
the operands for that operator
-,+
1,2,3,4
Operator Types
• Unary operator: An operator that has only one operand.
• Binary operator: An operator that has two operands
• In expression 3 + 4, 3 and 4 are the operands for the operator +. In
this expression, the operator + is a binary operator with two
operands.
• Moreover, in the expression +27, the operator + indicates that the
number 27 is positive. Here, + has only one operand and so acts as a
unary operator.
Order of 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,
*, /, and % are at a higher level of precedence than + and - Note that
the operators *, /, and % have the same level of precedence.
Similarly, the operators + and - have the same level of precedence.
• When operators have the same level of precedence, the operations
are performed from left to right
Example
• For example, using the order of precedence rules,
• 3*7-6+2*5/4+6
means the following:
• (((3 * 7) - 6) +((2 * 5) / 4)) + 6 = ((21 - 6) + (10 / 4)) + 6 (Evaluate *)
• = ((21 - 6) + 2) + 6 (Evaluate /.
• Note that this is an integer division.)
• = (15 + 2) + 6 (Evaluate -)
• = 17 + 6 (Evaluate first +)
• = 23 (Evaluate +)
Example of Arithmetic Operators in C++
Obtained Output
Expressions
Allocating Memory with Constants and
Variables
• The main objective of a C++ program is to perform calculations and
manipulate data. Recall that data must be loaded into the main
memory before it can be manipulated. In this section, you will learn
how to put data into the computer’s memory. Storing data in the
computer’s memory is a two-step process:
• 1. Instruct the computer to allocate memory.
• 2. Include statements in the program to put data into the allocated
memory
Continue..
• When you instruct the computer to allocate memory, you tell it not
only what names to use for each memory location, but also what type
of data to store in those memory locations. Knowing the location of
data is essential because data stored in one memory location might
be needed at several places in the program.
• cin is an object of the iostream class, which is used to extract
data from the standard input stream (usually the keyboard).
• cout is an object of the iostream class, which is used to send
data to the standard output stream
Example
• #include <iostream>
• using namespace std;
• int main()
• {
• int num1, num2;
• cout << "Welcome to the codedamn calculator!" << endl;
• cout << "Enter two numbers: ";
• cin >> num1 >> num2;
• cout << "The sum of the two numbers is: " << num1 + num2 << endl;
• return 0;
• }
the cout statement is used to display a welcome
message and prompt the user to enter two numbers,
which are then read from the keyboard using
the cin statement. The cin statement extracts the two
numbers from the standard input stream and stores them
in the num1 and num2 variables. The cout statement then
outputs the sum of the two numbers to the screen.

cin can be used to extract values of different types (e.g., int, double, string) from the input stream using the >> operator.

You might also like