PF Lab 3
PF Lab 3
PF Lab 3
LAB #04:
IF Statement, IF-Else & Else-IF Statements, Nested
IF-Else Statements, Recap of Operators.
Lab Objective:
● Recap of Operators
Lab Description:
Bitwise operators
Programming Fundamentals Cyber Security Muhammad Jalal Shah
#include <iostream>
main() {
int c = 0;
c = a | b; // 61 = 0011 1101
c = a ^ b; // 49 = 0011 0001
return 0;
}
Programming Fundamentals Cyber Security Muhammad Jalal Shah
To specify the conditions under which a statement or group of statements should be executed.
if (testExpression)
// statements
Flowchart of if Statement
if...else
The if else executes the codes inside the body of if statement if the test expression is true and
skips the codes inside the body of else. If the test expression is false, it executes the codes inside
the body
of else statement and skips the code inside of if
Programming Fundamentals Cyber Security Muhammad Jalal Shah
How if...else statement works?
Flowchart of if...else
The if...else statement executes two different codes depending upon whether the test expression
is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The nested if...else statement allows you to check for multiple test expressions and execute
different codes for more than two conditions.
Programming Fundamentals Cyber Security Muhammad Jalal Shah
if (testExpression1)
else if(testExpression2) {
// statements to be executed if testExpression1 is false and testExpression2 is true
else if (testExpression 3) {
if( boolean_expression 1)
}
Programming Fundamentals Cyber Security Muhammad Jalal Shah
Example : Nested If
We can nest else if…else in the similar way as you have nested if statement.
int number=0;
if (number==50 )
cout << "B grade total number is =: " << number << endl;
cout << "B grade total number is =: " << number << endl;
else {
cout << " Enter your marks correctly " << endl;
}
Introduction to programming Department of Cyber Security Azhar Ghafoor
Task1:
If the user enters a negative number print number entered is positive otherwise print number
is negative.
Task2:
Task3:
Input : Mark
If mark less than 75 and greater than and equal to 60, score will be
B
If mark less than 60 and greater than and equal to 45, score will be
C
If mark less than 30, score will be D Output : Print the grade of your
score.