Professional Documents
Culture Documents
n Operator
n What
n Types of operators
n Operator rules
Example – adding 2 numbers
n Yousof: Hey Khalifa, I just learned how to add two numbers
together.
n Khalifa: Cool!
n Yousof : Give me the first number.
n Khalifa: 2.
n Yousof : Ok, now give me the second number.
n Khalifa: 5.
n Yousof : Ok, here's the answer: 2 + 5 = 7.
n Khalifa: Wow! Yousof you are AMAZING!
after Khalifa says “2”, Yousof has to keep this number in his mind.
after Khalifa says “5”, Yousof also needs to keep this number in his
mind.
First number: 2 Second number: 5 Sum: 7
Variables
n A variable refers to memory location associated
with a memory address where a value of a
specified type may be stored
n The value of a variable can be changed during the
program execution
n The variable is referred to by a name
Variable: what for?
integer1
Integer_1
Examples:
const float pi = 3.1415;
float const pi = 3.1415;
Variables: Exercise-3
n Using the variables of exercise-2, write
a program that computes the area of
circle. Assume the following:
n The program uses a constant for PI
n The value of radius is assigned by the
programmer at the initialization
Variables: Exercise-3
#include <iostream>
using namespace std;
const float PI = 3.1415; // PI constant declaration
int main()
{
float Area =0 ; //declaration and initialization of the area
float Radius =2.5 ; // declaration of the variable and assigning of
//a value to it by the programmer
Area = PI*Radius*Radius ;
cout << “The Area is ”<< Area<<endl ; // prints the Area
return 0;
}
Variable Initialization (2)
n Second Way (by the user): using Standard Input Stream
object cin (see-in) with the stream extraction operator >>:
n >> is used with cin (see-in)
n It waits for user to input value (the keyboard by default), then
process the data once the Enter (Return) key has been pressed
n It stores value in variable to right of operator
n Skips over white spaces (space, tab, newline).
n Example:
int num1;
cin >> num1;
20
Example
n Write a C++ program that reads two integer
numbers add them and print the result of
addition
return 0;
}
Correct version
/* This is a practice example
Solution . */
#include <iostream>
using namespace std;
int main()
{ int num1, num2, sum;
cout << “ Please enter two numbers \n“ ;
cin >> num1;
cin >> num2;
/* or you can write: cin >> num1 >> num2; */
sum= num1 + num2;
cout << “The sum is “<<sum<<endl ;
return 0;
}
Memory Concept
num1 4
cin >> num1;
– Assume user entered: 4
num1 4
num2 5
cin >> num2;
– Assume user entered: 5
num1 4
num2 5
sum = num1 + num2; sum 9
Operators
n Symbols telling the compiler to perform
a specific arithmetic or logical
operations
n Arithmetic operators
n Assignment operators
n Logical operators
n And more…
Arithmetic operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
C++ operation Arithmetic Algebraic C++ expression Example
operator expression
Addition + x+7 x + 7 x = 4 + 2;
Subtraction - x-y x – y x = 4 - 2;
Multiplication * x*y x * y x = 4 * 2;
Division / x / y x / y x = 4 / 2;
Modulus % x mod y x % y x = 4 % 2;
Modulus (%)
n Modulus (%) returns the remainder of the division
n cout<< 3 % 2; // evaluates to 1
n cout<< 4 % 2; // evaluates to 0
n cout<< 6 % 2; // evaluates to 0
n cout<< 7 % 2; // evaluates to 1
n cout<< 8.0%2; // compiler error
Notes:
- It is not the equality operator
- It assigns the value (result) of the expression to a variable
Examples:
Area = Side*Side ;
Average = (Num1 + Num2)/2 ;
Num = 7 ;
Assignment operator =
Rule: The left side and the right side of an assignment
operation must be of the same type
Num2 = 3.75 ;
What is wrong in this code?
Num1 = Num2 ;
Operator precedence
n5+Num*7
Which operation is performed first ?
37
Comparison operators
Operator Meaning
=========================
< less than
> greater than
<= less than or equal
>= greater than or equal
== equals to
!= not equals to
Comparison operators
n Comparison operators used to form conditions that are evaluated by
the compiler to either true or false
n Example: assume num1 = 3 and num2 =5, how the compiler will
evaluate each of these expressions:
num1 < num2
num1 <= num2
num1 > num2
num1 >= num2
num1 == num2
num1 != num2
Increment/Decrement operators
Increment operator ++
This operator adds 1 to a variable
Num++ ; // is equivalent to Num = Num +1 ;
Decrement operator --
This operator subtracts 1 from a variable
Num-- ; // is equivalent to Num =Num -1 ;
Operators Precedence (2)
Operator(s) Operation(s) Order of evaluation (precedence)