0% found this document useful (0 votes)
22 views

Week 2 - Programming Fundamentals

The document discusses various logical operators and control flow statements in C++ programming. It explains truth tables and examples of logical operators like NOT, AND, OR. It also covers assignment, increment, decrement operators and conditional statements like if, if-else, nested if-else.

Uploaded by

Muazzam Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Week 2 - Programming Fundamentals

The document discusses various logical operators and control flow statements in C++ programming. It explains truth tables and examples of logical operators like NOT, AND, OR. It also covers assignment, increment, decrement operators and conditional statements like if, if-else, nested if-else.

Uploaded by

Muazzam Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

3/10/2023 

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


3/10/2023 

TRUTH TABLE OF NOT

A (Input) X (Output)
0 1
1 0

Example:
cout << (!true);
cout << (!false);
3

NOT OPERATOR


3/10/2023 

TRUTH TABLE OF AND


A (Input) B (Input) X (Output)
0 0 0
0 1 0
1 0 0
1 1 1

For Example:
cout << (false && false);
cout << (false && true);
cout << (true && false);
cout << (true && true);

&& OPERATOR


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


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

INCREMENT AND DECREMENT OPERATOR


Operator  Description  Example 
Increment operator,
++  increases integer A++ will give 11 
value by one 
Decrement
operator, decreases
--  A-- will give 9 
integer value by
one 
10


3/10/2023 

PRE AND POST INCREMENT OPERATOR


Pre-increment operator: A pre-increment operator is used
to increment the value of a variable before using it in an
expression. In the Pre-Increment, value is first incremented
and then used inside the expression.
Post-increment operator: A post-increment operator is
used to increment the value of the variable after executing
the expression completely in which post-increment is used.
In the Post-Increment, value is first used in an expression
and then incremented.
11

PRE AND POST INCREMENT OPERATOR


Example 1: Output
int x = 10;
10
cout << x++;

Example 1: Output
int x = 10;
cout << ++x; 11

12


3/10/2023 

PRE AND POST DECREMENT OPERATOR


Example 1: Output
int x = 10;
10
cout << x--;

Example 1: Output
int x = 10;
cout << --x; 9

13

PRE AND POST DECREMENT OPERATOR


#include <iostream> #include <iostream>
using namespace std; using namespace std;

int main(){ int main(){


int a = 5; int a = 5;
int b = ++a; int b = a++;

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


3/10/2023 

PRE AND POST DECREMENT OPERATOR


int a, b, c, d, x;
a = 5;
b = 7;
c = 12;
d = 15;
x = ++a + ++b + c++ + d++;
cout << "Value of a is " << a << endl;
cout << "Value of b is " << b << endl;
cout << "Value of c is " << c << endl;
cout << "Value of d is " << d << endl;
cout << "Value of x is " << x << endl;
15

FLOW OF EXECUTION

16


3/10/2023 

EXAMPLES

17

LOGICAL OR BOOLEAN EXPRESSION


• A condition is represented by logical(boolean) expression.
• An expression that has a value of either true or false.
• Example: a>b

18


3/10/2023 

NOTE!

19

if: One-Way Selection

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 

if…else : Two-way selection

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

1. C++ Program to Check Whether Number is Even or Odd

2. C++ Program to Check Whether a character is Vowel or


Consonant.

25

Multiple Selections: Nested If…else

26

13 
3/10/2023 

Nested If…else: Example 01


#include<iostream> 
using namespace std;  
int main() 
{   
  int score; 
  cout<<“Enter your score:”; 
  cin>>score; 
 
      
       if (score>=90)        
           cout<<“Your grade is A!”; 
  else  
           if (score>=80) 
           cout<<“Your grade is B!”; 
       else 
           if(score>=70) 
      cout<<“Your grade is C!”; 
       else 
           if(score>=60) 
      cout<<“Your grade is D!”; 
       else 
              cout<<“Your grade is F!”;        
      
 
return 0; 
 
}  27

Nested If…else: Example 02


#include<iostream> 
using namespace std;  
int main() 
{   
   
 
      
       if (day==‘Monday’)  
           if(time==‘8:30’) 
             cout<<“Time for Programming Lab!”; 
           else 
             cout<<“Relax!”; 
       else if(day==‘Tuesday’) 
            if(time==‘9:30’) 
               cout<<“Time for Programming Class!”; 
       else 
               cout<<“See you next week!”; 
             
 
      
 
return 0; 
 

28

14 
3/10/2023 

Nested If…else: Example 02


#include<iostream> 
using namespace std;  
int main() 
{   
  WHICH DATA TYPE?
 
      
       if (day==‘Monday’)  
           if(time==‘8:30’) 
             cout<<“Time for Programming Lab!”; 
           else 
             cout<<“Relax!”; 
       else if(day==‘Tuesday’) 
            if(time==‘9:30’) 
               cout<<“Time for Programming Class!”; 
       else 
               cout<<“See you next week!”; 
             
 
      
 
return 0; 
 

29

Programming Activity
Re-write Example 02 using && operator.

30

15 
3/10/2023 

Nested If…else vs. a Series of if statements

If…else statements Sequence of If statements


31

16 

You might also like