Week 2 - Programming Fundamentals
Week 2 - Programming Fundamentals
PROGRAMMING FUNDAMENTALS
Course Instructor:
Engr. Adnan Wahid
Senior Lecturer
Credentials:
MS(Computer Science & IT), NED University
ME(Electronics), NED University
BS (Electronic Engineering), Sir Syed University
1
LOGICAL OPERATORS
•
1
3/10/2023
A (Input) X (Output)
0 1
1 0
Example:
cout << (!true);
cout << (!false);
3
NOT OPERATOR
•
2
3/10/2023
For Example:
cout << (false && false);
cout << (false && true);
cout << (true && false);
cout << (true && true);
&& OPERATOR
•
3
3/10/2023
TRUTH TABLE OF OR
A (Input) B (Input) X (Output)
0 0 0
0 1 1
1 0 1
1 1 1
For Example:
cout << (false || false);
cout << (false || true);
cout << (true || false);
cout << (true || true);
|| OPERATOR
•
4
3/10/2023
ASSIGNMENT OPERATOR
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
9
5
3/10/2023
Example 1: Output
int x = 10;
cout << ++x; 11
12
6
3/10/2023
Example 1: Output
int x = 10;
cout << --x; 9
13
cout << "a=" << a << endl; cout << "a=" << a << endl;
cout << "b=" << b << endl; cout << "b=" << b << endl;
return 0; return 0;
} }
a=6 a=6
b=6 b=5
14
7
3/10/2023
FLOW OF EXECUTION
16
8
3/10/2023
EXAMPLES
17
18
9
3/10/2023
NOTE!
•
19
20
10
3/10/2023
if: Example 01
#include<iostream>
using namespace std;
int main()
{
int score;
cout<<“Enter your score:”;
cin>>score;
if (score>=60)
{
cout<<“You have passed!”;
}
return 0;
}
21
if: Example 02
#include<iostream>
using namespace std;
int main()
{
int score;
char grade;
cout<<“Enter your score:”;
cin>>score;
if (score>=60)
{
grade=‘P’;
cout<<“You have passed!”;
}
return 0;
}
22
11
3/10/2023
23
If…else: Example 01
#include<iostream>
using namespace std;
int main()
{
int score;
cout<<“Enter your score:”;
cin>>score;
if (score>=60)
cout<<“You have Passed!”;
else
cout<<“You have Failed!”;
return 0;
}
24
12
3/10/2023
SOME TASKS
25
26
13
3/10/2023
14
3/10/2023
Programming Activity
Re-write Example 02 using && operator.
30
15
3/10/2023
31
16