Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Lecture - 1 (Structure Programming)

bitBox Online IT Job Care

■ Introduction
■ Data type
■ Variable
■ Operator

Instructor: Engr. Harun Or Roshid, Phone: 01740014400, Facebook: fb.com/engrharun400


Subject Overview

Programming Part (Both Structure & OOP)

• Output নির্ণয়ের Program


• Half Complete Program (এক বা একানিক Function নিখয়ে বয়ি)
• Complete Program etc.
Data Type, Variable, Operator

Arithmetic Operator:

What will be the output of the following Explain:


arithmetic expression? =5+((3*2)%10)-(8*6)
=5+ (6%10)-48
5+3*2%10-8*6 =5+6-48
=11-48
Output: -37 =-37
Data Type, Variable, Operator

Increment/Decrement Operator: Explain:


#include <stdio.h>
Find The Output of the program: int main(void) {
if i=0; int i=0;
i = i++ + i++ + i++ + i++; i = (((i++ + i++) + i++) + i++); \\left to right
then i=? /*
i = (((0++ + 0++) + i++) + i++); \\but i=2
i = ((0 + i++) + i++);
Output: 6 i = (0 + 2++) + i++); \\but i=3
i = (2 + 3++);
i = (2 + 3++); \\i=6
*/
printf("%d", i);
return 0;
}
Data Type, Variable, Operator

Increment/Decrement Operator:

Find output: Explain:


Int p=0;k=0; P = ((++k (1)(2) + ++k (2)) + ++k) // k=2
p=++k+ ++k+ ++k P = (4 + ++k(3)) // k=3
p=? P=7

Output: 7

Find output: Explain:


Int i =0; I = ((i++ + i++) + i++) //i=2
i=i+++i+++i++ I = (1+ (2)i++) //i=3
i=? I=3

Output : 3
Data Type, Variable, Operator

Relational Operator:

Explain:
Data Type, Variable, Operator

Logical Operator:

Find out the output of the following program: Explain:


#include<iostream> ++i && ++j এর পরর শর্ত টা মিথ্যা হরে যাে,
using namespace std; (কারন ++i=-19 and ++j=0 hoy), র্াই পররর
int main(){ ++k আর রান হে না র্াই k এর িান আরের
int i=-20, j=-1, k=2, m; টাই থ্ারক, স া, i=-19, j=0, k=2, এবং m=0 হে
m=++i&&++j&&++k; কারন শর্ত টি মিথ্যা
cout<<i<<" "<<j<<" "<<k<<" "<<m;
return 0;
}
Output: -19 0 2 0
Data Type, Variable, Operator

Logical Operator:

Find the output of the following program. Explain:


int main(){
int i=1,j=1,k=1; Step 1: int i=1, j=1, k=1;
cout <<++i || j++ && ++k; Step 2: here (j++ && ++k;) this code will not
cout<<i<<j<<k; get executed because ++i has non-zero
return 0; value.
} becomes 2 || j++ && ++k;
Output: 2211 Step 3: In the previous step the value of
variable 'i' only incremented by '1'(one).
The variable j,k are not incremented.
Having higher precedence does not mean it gets evaluated first. It just means it binds tighter.
In that example, that expression is equivalent to: ++i || (++j && ++k). What gets evaluated first is ++i because || evaluates left to right.
Only if that evaluates to false will ++j && ++k be evaluated because || is short-circuiting.
Short circuit evaluation. If, the left-side of the && is non-zero, only then will the right-hand side be evaluated.
Likewise, only if the left-hand side of the || is zero, will the right-hand side be evaluated.
Data Type, Variable, Operator

Conditional Operator: Explain:


Part-1 Part-2
Write the output of the following program?
#include<stdio.h>
int main(){
int a=0, b=1, c=2; Assignment operator এ right to left এ কাজ কয়র। োই পার্ণ ২
আয়ে কাজ হয়ব। পার্ণ ২ এ একটি কনিশিাি অপায়রর্র বযবহার করা হয়েয়ে।
*((a)?&b:&a)=a?b:c;
আমরা জানি কনিশিাি অপায়রর্য়র একর্া শেণ থায়ক যেটি সেয হয়ি প্রথম
printf("%d %d %d", a,b,c);
অংশ কাজ কয়র আর নমথযা হয়ি পয়রর অংশ কাজ কয়র (শেণ ? সেযঃ নমথযা;)
return 0; পার্ণ ২ এ যেখয়ে পারয়েনে a=o (অথণাৎ False) োই কনিশয়ির পয়রর অংশ
} কাজ করয়ব। c এর মাি ২ োই পার্ণ ২ এর অপায়রশি যশষ হয়ি ফিাফি পাব
২। এখি এই ২ আসযাইি হয়ব পার্ণ ১ এর ময়িয, পার্ণ ১ এ আবার একটি
Output: 2 1 2 কনিশিাি অপায়রর্র বযবহার করা হয়েয়ে। যসখায়িও a এর ভ্যািু ০ (অথণাৎ
False) োই পয়রর অংশ কাজ করয়ব অথণাৎ &a এইর্া select হয়ব। পার্ণ ২ এ
যপিাম ২ আর পার্ণ ১ এ যপিাম &a । োই a ঠিকািাে ২ assign হয়ব (অথণাৎ
a এর মাি ০ যথয়ক পনরবেণ ি হয়ে ২ হয়ে োয়ব)।
োই অউর্পু র্ ফিাফিঃ ২ ১ ২ যেখায়ে।
Data Type, Variable, Operator

bitwise Operator: Explain:

Int x = ~1 //bitwise operator


What is the value of 'x’?
Index -> 128 64 32 16 8 4 2 1
int x = ~1;
Value -> 0 0 0 0 0 0 0 1
Output: -2 1’s com of 1 (~1) 1 1 1 1 1 1 1 0

Index -> 128 64 32 16 8 4 2 1


Value -> 0 0 0 0 0 0 1 0
2’s com of 2 (~2) 1 1 1 1 1 1 1 0

The one’s compliment of 1 is 1110 (binary) which is equivalent to two’s


compliment of 2, ie -2.
Data Type, Variable, Operator
bitwise Operator:

Example: Bitwise AND Explain:


#include <stdio.h> 12 = 00001100 (In Binary), 25 = 00011001 (In Binary)
int main(){
int a = 12, b = 25; Bit Operation of 12 and 25
printf("Output = %d", a&b); 00001100
return 0; & 00011001
} ___________
Output: 8 00001000 = 8 (In decimal)

Example: Bitwise OR Explain:


#include <stdio.h> 12 = 00001100 (In Binary), 25 = 00011001 (In Binary)
int main(){
int a = 12, b = 25; Bitwise OR Operation of 12 and 25
printf("Output = %d", a|b); 00001100
return 0; | 00011001
} ___________
Output: 29 00011101 = 29 (In decimal)
Data Type, Variable, Operator

bitwise Operator:

Example: Bitwise XOR Explain:


#include <stdio.h> 12 = 00001100 (In Binary)
int main(){ 25 = 00011001 (In Binary)
int a = 12, b = 25;
printf("Output = %d", a^b); Bitwise XOR Operation of 12 and 25
return 0; 00001100
} ^ 00011001
__________
Output: 21 00010101 = 21 (In decimal)
Data Type, Variable, Operator

bitwise Operator: Left Shift Operator (<<) : Takes two numbers, left shifts the bits of the first operand, the
second operand decides the number of places to shift. In other words, left shifting an integer ‘x’ with an integer ‘y’
(x<<y) is equivalent to multiplying x with 2^y (2 raise to power y). When Data is Shifted Left , trailing digits are filled
with zero.

Example for Left Shift: Explain:


#include<stdio.h> a=5(00000101)
int main(){
b=9(00001001)
unsigned char a = 5, b = 9;
printf("a<<1 = %d\n", a<<1); 1st Print: result is 10(00001010)
printf("b<<1 = %d\n", b<<1); 2nd Print: result is 18(00010010)
return 0;
}
Output:
a<<1 = 10
b<<1 = 18
Data Type, Variable, Operator

bitwise Operator: Right Shift Operator (>>) : Takes two numbers, right shifts the bits of the first operand, the second
operand decides the number of places to shift. In other words, right shifting (x>>y) is equivalent to dividing x with 2^y. When Data
is Shifted Left , digits from right are filled with zero.

Example for Right Shift: Explain:


#include<stdio.h> a=5(00000101)
int main(){
b=9(00001001)
unsigned char a = 5, b = 9;
printf("a>>1 = %d\n", a>>1); 1st Print: result is 2(00000010)
printf("b>>1 = %d\n", b>>1); 2nd Print: result is 4(00000100)
return 0;
}
Output:
a>>1 = 2
b>>1 = 4
Important Points :
•The left shift and right shift operators should not be used for negative numbers. The result of
is undefined behavior if any of the operands is a negative number.
•For example results of both -1 << 1 and 1 << -1 is undefined.
•If the number is shifted more than the size of integer, the behavior is undefined.
•For example, 1 << 33 is undefined if integers are stored using 32 bits.
Data Type, Variable, Operator

bitwise Operator:

What will be the output of the program? Explain:


#include<stdio.h> res = (64>>1)&(~4)
int main(){ res = 32&~(100)
unsigned int res; res = (100000 & 111011) // যেয়হেু নবর্ওোইজ
res = (64>>(2+1-2))&(~(1<<2)); অপায়রর্র োই ইকুইভ্যায়িন্ট বাইিানর নবর্ নহসায়ব কাজ হয়ব।
printf("%d\n",res); res = 32
return 0;
}

Output: 32
Thank You!
Any questions?

Instructor: Engr. Harun Or Roshid, Phone: 01740014400,


Facebook: fb.com/engrharun400

You might also like