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

Computer Programming - I (MCT-143) Lab Session 6 Nested If-Else

The document discusses nested if-else statements in computer programming. It provides examples of nested if-else statements to model real-world decision making scenarios like choosing transportation options. It also explains the importance of proper indentation to clearly show the nesting of if-else blocks. Some programming tasks are given to practice nested if-else statements, like finding the maximum of three numbers or checking student grades based on average marks.

Uploaded by

Ahmed Sajid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Computer Programming - I (MCT-143) Lab Session 6 Nested If-Else

The document discusses nested if-else statements in computer programming. It provides examples of nested if-else statements to model real-world decision making scenarios like choosing transportation options. It also explains the importance of proper indentation to clearly show the nesting of if-else blocks. Some programming tasks are given to practice nested if-else statements, like finding the maximum of three numbers or checking student grades based on average marks.

Uploaded by

Ahmed Sajid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer Programming – I (MCT-143)

Lab Session 6
Nested if-else
You are familiar with if and if-else statements. There can be a case that the block of if or else further
contain some other if or if-else statement.

Even though it is a very simple concept but students face problem to understand it.

Let’s try to get the idea from daily life example:

Example:
You got an emergency to visit Islamabad within next 7 hours. You decided to check if some flight is
available flight otherwise (else) you will go in Bus. In flow chart it can be shown as:

In Computer Programming style the situation can be written as:

if (flight available){

Take the flight}

else{

Take the Bus}

Suppose three airlines are providing services from LHR to ISL which are PIA, Emirates and QATAR airlines.
Now you decide that if flight is available, you will prefer Emirates or otherwise (else) you will choose
QATAR Airline, and if that too is not available then (else) you will select PIA. In flow chart it can be shown
as:
In Computer Programming style the situation can be written as:

if (flight available){

if (Emirates Available){

Take it}

else{

if (QATAR Airline Available){

Take it}

else{

Take PIA}

else{

Take the Bus}

Now you also decide about the possibilities of Bus. You prefer to travel by Motor Way than G.T Road. In
flow chart it can be shown as:
In Computer Programming style the situation can be written as:

if (flight available){

if (Emirates Available){

Take it}

else{

if (QATAR Airline Available){

Take it}

else{

Take PIA}

else{

if (MW Bus available){

Take the Bus}

else {

Take the G.T Road Bus

}
In writing nested if-else (and many other nested statements to cover later on) you must take care of
indentation to clearly differentiate outer and nested statements. The difference in code (in this case
pseudo code) with indentation and without indentation can be seen in table below:

With Indentation Without Indentation


if (flight available){ if (flight available){
if (Emirates Available){ if (Emirates Available){
Take it} Take it}
else{ else{
if (QATAR Airline Available){ if (QATAR Airline Available){
Take it} Take it}
else{ else{
Take PIA} Take PIA}
else{ else{
if (MW Bus available){ if (MW Bus available){
Take the Bus} Take the Bus}
else { else {
Take the G.T Road Bus Take the G.T Road Bus
} }
} }

Both columns above have exactly same logic but in second column (without indentation) the logic seems
messed up.

Task 1:
You have to repeat the Task 5 of last lab session using nested if-else statements. The task was:
Write a program that will take three integers as input and will print the maximum of three numbers at
output.

In last lab session you used three if statements like if(a>b && a>c). But this time used nested if-else
statements and compare ONLY two values in any if condition.

Sample output is:


Enter first integer: 12
Enter second integer: 16
Enter third integer: 5

Second number 16 is the largest

if-else-if:
It is not a new thing and you should understand it easily. Remember the task of taking five subject marks
from user; displaying the average and then displaying a message based on average marks. The last
message was displayed as follow:

if (Avg>=80) {

cout << "\nYou are outstanding student.";

}
if (Avg>=70 && Avg<80) {

cout << "\nYou are good student.";

if (Avg>=60 && Avg<70) {

cout << "\nYou are an average student.";

if (Avg>=50 && Avg<60) {

cout << "\nYou are below average student.";

if (Avg>=40 && Avg<50) {

cout << "\nYou are poor student.";

if (Avg<40) {

cout << "\nYou need extra ordinary efforts.";

You know that why you need to write condition as if (Avg>=70 && Avg<80) instead of just if (Avg>=70). But
you can use if-else-if to do the same task as:

if (Avg>=80) {

cout << "\nYou are outstanding student.";

else if (Avg>=70) {

cout << "\nYou are good student.";

else if (Avg>=60) {

cout << "\nYou are an average student.";

else if (Avg>=50) {

cout << "\nYou are below average student.";

}
else if (Avg>=40) {

cout << "\nYou are poor student.";

else {

cout << "\nYou need extra ordinary efforts.";

}
for Loop

Task 2:
Write a program that will print number 1 to 7 and their cubes separated by a tab using for loop.

The output is:


1 1
2 8
3 27
4 64
5 125
6 216
7 343

Task 3:
Modify above code such that it asks user for the number and prints 1 to that number with their cubes.

Sample output is:


Enter a number: 4
1 1
2 8
3 27
4 64

Task 4:
You have to repeat Task 3 of previous Lab Session. But this time you have to use for loop to take marks of
five subjects from user. Also used if-else-if instead of if-else as discussed above.

Write a program that will ask for the marks of five subjects one by one and will print the average of the
marks. Moreover a message will be printed out depending upon average marks as:
If average is greater than or equal to 80  You are outstanding student.
If average is greater than or equal to 70 but less than 80  You are good student.
If average is greater than or equal to 60 but less than 70  You are an average student.
If average is greater than or equal to 50 but less than 60  You are below average student.
If average is greater than or equal to 40 but less than 50  You are poor student.
If average is less than 40 You need extra ordinary efforts

Max marks are 100 and you can assume that marks are of integer data type.

Sample output is:


Enter subject 1 marks: 74
Enter subject 2 marks: 68
Enter subject 3 marks: 82
Enter subject 4 marks: 65
Enter subject 5 marks: 87
Average of five subject marks is: 75.2
You are a good student

Practice Problems:

I STRONGLY recommend trying these problems at home before coming to next Lab on Wednesday.

Task 1:

Write a program that prints the multiplication table of 12 from 1 to 10 using for loop as shown below:
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

Task 2:
Modify above program such that it takes two inputs from user. First the number whose multiplication table
user wants at output and second the final limit of the table e.g. final limit of above table was 10.

Sample output is:


Which multiplication table you want: 5
What is final limit: 12

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60
Task 3:
Write a program that will take a number from user and will print its factorial at output. You have to take
care of negative number and zero as well.
Sample output is:
Enter a number: 4
4!=24

Another sample output is:


Enter a number:-6
Factorial of negative number does not exists

Another sample output is:


Enter a number: 0
0!=1

How to do it:
You will take input from user and apply three conditions. If entered number is zero, display 0!=1; if entered
number is negative, display the message Factorial of negative number does not exists; and if number is
greater than zero, execute a for loop to find its factorial. You will start the loop from entered number and
keep on decrementing till one. You can do reverse as well i.e. start from one and increment till number
entered. Inside loop body, you will multiply variable to update it (which stores answer for factorial), as you
updates Sum variable in Task 4.

You might also like