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.
integer1
Integer_1
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
int main()
Notice how cin is used to get the user input.
{ int num1, num2, sum; //declaration
#include <iostream>
void main()
Solution . */
#include <iostream>
int main()
num2 5
num2 5
sum 9
sum = num1 + num2;
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
% Modulus
C ++ o p e ra tio n Arith m e tic Alg e b ra ic C ++ e xp re ssio n Exa m p le
o p e ra to r e xp re ssio n
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;
left-side right-side
double Num2 ; =
Num2 = 3.75 ;
What is wrong in this code?
Num1 = Num2 ;
Operator precedence
5+Num*7
Which operation is performed first ?
36
Logical Operators
Examples
37
Comparison operators
Operator Meaning
=========================
< less than
> greater than
<= less than or equal
>= greater than or equal
== equals to
!= not equals to
Comparison operators
Comparison operators used to form conditions that are evaluated by
the compiler to either true or false
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
Decrement operator --