0% found this document useful (0 votes)
18 views33 pages

Lecture 3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 33

Glory be to you, we have no knowledge except what you have taught us.

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.

C++ provides a variety of operators. These include:


Arithmetic operators (+, -, *, /)
Relational operators, (<, >, <=, !=, == )
Logical operators, (&&, ||, ! )
Bitwise operators etc.
The operators can be categorized as follows:
Unary Operator
Binary Operator

08/10/2024 9
Increment Operator

08/10/2024 Programming Fundamentals Week #2 11


Example:

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

1. *, /, % Do all multiplications, divisions and remainders from left to


right.
2. +, - Do additions and subtractions from left to right.

08/10/2024 Programming Fundamentals Week #2 18


Evaluating an Expression
6+2*3/6
• Three operators are in this expression.
• However, * and / both have the same precedence and + has lower precedence
then these two.
• * and / will be evaluated first but both have the same precedence level.
• Therefore, operator associatively will be used here to determine the
first to get evaluated i.e., left to right.
• The left most sub expression will be evaluated followed by the next right one
and so on.

• * will be evaluated first then /

08/10/2024 19
Arithemetic Operator
Type Operators Usage
Arithmetic ‘+’ ‘-’ ‘*’ ‘/’ ‘%’ a+b a-b a*b a/b a%b

• The Modulus Operator


– % is known as the Modulus Operator or the Remainder Operator.
– It calculates the remainder of two variables
– It can only be used with two ints..

– 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 Programming Fundamentals Week #2 22


Bitwise Operators
& (bitwise AND)
• Takes two numbers as operands and does AND on every bit of two numbers.
• The result of AND is 1 only if both bits are 1.
| (bitwise OR)
• Takes two numbers as operands and does OR on every bit of two numbers.
• The result of OR is 1 if any of the two bits is 1.
^ (bitwise XOR)
• takes two numbers as operands and does XOR on every bit of two numbers.
• The result of XOR is 1 if the two bits are different.

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.

08/10/2024 Programming Fundamentals Week #2 24


Output of following program
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int num1=20; 20
int num2=30; 61s
123
num2+=30; 63
cout<<num1<<endl; 61
num1=++num2; 10
cout<<num1++<<endl;
num1+=num2--;
cout<<num1<<endl;
num1-=num2++;
cout<<num1<<endl;
cout<<num2<<endl;

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

Explicit Typecasting is performed by programmer.


static_cast Keyword OR above syntax is used to
perform explicit Type Casting in C++.

While in C, only above syntax works. static_cast is


not used in C.
Local Variable

31
Global Variable
Static Variable

You might also like