Computer Programming - I (MCT-143) Lab Session 6 Nested If-Else
Computer Programming - I (MCT-143) Lab Session 6 Nested If-Else
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.
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:
if (flight available){
else{
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{
Take it}
else{
Take PIA}
else{
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{
Take it}
else{
Take PIA}
else{
else {
}
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:
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.
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) {
}
if (Avg>=70 && Avg<80) {
if (Avg<40) {
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) {
else if (Avg>=70) {
else if (Avg>=60) {
else if (Avg>=50) {
}
else if (Avg>=40) {
else {
}
for Loop
Task 2:
Write a program that will print number 1 to 7 and their cubes separated by a tab using for loop.
Task 3:
Modify above code such that it asks user for the number and prints 1 to that number with their cubes.
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.
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.
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
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.