Variables and Operators: C++ Basics
Variables and Operators: C++ Basics
C++ BASICS
IN THIS LECTURE WE WILL SEE
• Variable
• What
• Declaration
• Initialization
• By the programmer
• Input by the user
IN THIS LECTURE WE WILL SEE
(CONT)
• Operator
• What
• Types of operators
• Operator rules
EXAMPLE – ADDING 2 NUMBERS
• Yousof: Hey Khalifa, I just learned how to add two numbers together.
• Khalifa: Cool!
• Yousof : Give me the first number.
• Khalifa: 2.
• Yousof : Ok, now give me the second number.
• Khalifa: 5.
• Yousof : Ok, here's the answer: 2 + 5 = 7.
• 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
integer1
Integer_1
float Radius,
float Radius
Area ;
=0 ;
Radius =0;
float Area =0 ;
Area = 0;
CONSTANT DECLARATIONS
• Constants are used to store values that never change during the
program execution.
• Constant variables must be initialized at the same time of
declaration
• Constants must be declared before the main function
• Syntax:
const <type> <identifier> = <expression>;
<type> const <identifier> = <expression>;
Examples:
const float pi = 3.1415;
float const pi = 3.1415;
VARIABLES: EXERCISE-3
• Using the variables of exercise-2, write a program that computes the area of
circle. Assume the following:
• The program uses a constant for PI
• 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)
• Second Way (by the user): using Standard Input Stream
object cin (see-in) with the stream extraction operator >>:
• >> is used with cin (see-in)
• It waits for user to input value (the keyboard by default), then
process the data once the Enter (Return) key has been pressed
• It stores value in variable to right of operator
• Skips over white spaces (space, tab, newline).
• Example:
int num1;
cin >> num1;
20
EXAMPLE
• Write a C++ program that reads two integer numbers add
them and print the result of addition
• The program must ask the user to enter the first number
and the second number
num1 4
num2 5
sum = num1 + num2;
sum 9
OPERATORS
• Symbols telling the compiler to perform a specific arithmetic or logical
operations
• Arithmetic operators
• Assignment operators
• Logical operators
• And more…
ARITHMETIC OPERATORS
+ Addition
- Subtraction
* Multiplication
/ Division
%C ++ o p eModulus
ra tio n Arith m e tic
o p e ra to r
Alg e b ra ic
e xp re ssio n
C ++ e xp re ssio n Exa m p le
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 (%)
• Modulus (%) returns the remainder of the division
• cout<< 3 % 2; // evaluates to 1
• cout<< 4 % 2; // evaluates to 0
• cout<< 6 % 2; // evaluates to 0
• cout<< 7 % 2; // evaluates to 1
• cout<< 8.0%2; // compiler error
Notes:
- It is not the equality operator
- It assigns the value 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
int Num1;
double Num2 ;
left-side = right-side
Num2 = 3.75 ;
What is wrong in this code?
Num1 = Num2 ;
OPERATOR PRECEDENCE
5+Num*7
Which operation is performed first ?
When Logical operations are evaluated they can only give true
(non zero) or false (zero) value.
LOGICAL OPERATORS
X Y !X X && Y X || Y
36
LOGICAL OPERATORS
Examples
37
COMPARISON OPERATORS
OperatorMeaning
=========================
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)
• (c*b % 2)
• False