Lecture 3
Lecture 3
Lecture 3
Verily, it is
You, the All Knower, the All-Wise.
Q[2:32]
Programming
Constants
• Constants are values which cannot be modified e.g., the value of Pi
• To declare a constant in C++, we write a keyword “const” before the
variable type.
const double pi = 3.14;
Types of Constant:
1. Literal Constant
2. Symbolic Constant
08/10/2024 3
Characters
• Character constants of type "char" must be enclosed in single quotation
marks
• char myChar = ‘A’;
08/10/2024 7
Reserved Words
• Some names cannot be declared as variable names because they are reserved
words in C++
20
08/10/2024 8
Operators
Operators are the symbols that are used to perform certain operations on data.
08/10/2024 9
Increment Operator
08/10/2024 13
Decrement Operator
Example:
08/10/2024 15
Output of following program
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int num1=20; OUTPUT:
int num2=30;
20
num2+=30; 61
cout<<num1<<endl; 123
num1=++num2; 63
cout<<num1++<<endl; 61
num1+=num2--;
cout<<num1<<endl;
num1-=num2++; 10
cout<<num1<<endl;
cout<<num2<<endl;
cout<<(5<<1)<<endl;
cout<<(5>>1);
} 16
Arithmetic Expression Evaluation
To evaluate an arithmetic expression two concepts needs to be
understood
- Operator Precedence
Operator precedence controls the order in which operations are performed
- Operator Associativity
The associativity of an operator specifies the order in which operations of
the same precedence are performed
08/10/2024 17
Operator Precedence and Associativity
Operators Precedence and Associativity for C++ is following
08/10/2024 19
Arithemetic Operator
Type Operators Usage
Arithmetic ‘+’ ‘-’ ‘*’ ‘/’ ‘%’ a+b a-b a*b a/b a%b
– 3%2 =1
– 5%2=1
– 6%3=0
– 8%5=3
08/10/2024 20
Arithmetic Operator Precedence and associativity
08/10/2024 21
Polynomials in C++
• In algebra
𝑎 + 𝑏 + 𝑐+ 𝑑 + 𝑒
= m
5
• In c++
𝑚 = (𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒)/5
• Algebra
C++ 𝑧 = 𝑝 ∗ 𝑟%𝑞 + 𝑤/𝑥 − 𝑦
08/10/2024 23
Bitwise Operators
<< (left shift)
• Takes two numbers, left shifts the bits of the first operand, the second operand
decides the number of places to shift.
>> (right shift)
• Takes two numbers, right shifts the bits of the first operand, the second
operand decides the number of places to shift.
~ (bitwise NOT)
• Takes one number and inverts all bits of it.
cout<<(5<<1)<<endl;
cout<<(5>>1);
} 26
Num1=62+61 =123
Num2=num2- -
=61-1=60
TypeCasting
08/10/2024 28
Implicit Type Casting:
Implicit Typecasting is performed
automatically by compiler
Explicit Type Casting
31
Global Variable
Static Variable