You are on page 1of 6

Programming Fundamentals

Lab Manual (Lab 5)

Topic: Nested if–else and if–else-if Statements

Course Instructor:
Lab Instructor:

Session: Spring 2021

School of Systems and Technology


UMT Lahore Pakistan
Objectives
The objective of the lab is to understand the concept and how to implement IF-Else statements and nested
IF-Else in C++. At the end of this lab students will be able to use all type of if-else statements.

Sample Program.
Write a program which takes marks as input and then shows output as follows:
Marks Output
87 – 100 Grade A
80 - 87 Grade B+
72 – 80 Grade B
67 – 72 Grade C+
60 - 67 Grade C
Below 60 Failed
Sample Code:

#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter marks";
cin>>marks ;

if (marks >= 87 && marks <=100 )


cout<<"Grade A\n";
else if (marks >= 80 && marks < 87)
cout<<"Grade B+\n";
else if (marks >= 72 && marks< 80)
cout<<"Grade B\n";
else if (marks >= 67 && marks < 72)
cout<<"Grade C+\n";
else if (marks >= 60 && marks< 67)
cout<<"Grade C\n";
else
cout<<"Failed\n";
return 0;
}
In above example logical operator && is used and because of this && operator condition will
only be satisfied if both the conditions in a single if are true. If any of the condition is false
because of && operator the whole condition will be treated as false.
Output Problems
 What is wrong with the following if statement. The indentation indicates the desired
behavior.

if (numNeighbors >= 3 || numNeighbors = 4) {

++numNeighbors;

cout << "You are dead!" << endl;

else {

--numNeighbors;

 Describe the output produced by this poorly indented program segment:

int number = 4;

double alpha = -1.0;

if (number > 0){

if (alpha > 0)

cout << "Here I am!" << endl;

else{

cout << "No, I’m here!" << endl;

cout << "No, actually, I’m here!" << endl;

}
Lab Tasks
Task 1:

Write a program, which takes age as input from user and prints appropriate message depending upon the
following conditions:
 
 If age less than 6 then prints, “What a nice child!”
 If age is between 6 and 9 then prints, “That’s a good age!”
 If age is between 9 and less than 20 then prints, “Ah! In the prime of life”
 If age between 20 and less than 30 then prints, “Watch out, the younger ones are gaining on
you.”
 More than 30 then it prints, “Well, have fun, and don’t look back.”

Task 2:

Write a program which takes 3 numbers as input then it finds the minimum and maximum number and
print output as follows:

Task 3:

Write a program in which it takes two numbers from keyboard as input and subtract larger
number from smaller?

Task 4:

Write a Program using Nested if-else structure. The program will get a Single Character from
user and Tell either it is Vowel or Not and it a capital or small letter?

Sample output:

Enter a character : A
A is a Vowel and it is Capital Letter.

Enter a character: y
y is not a Vowel and it is small Letter.
Task 5:

Write the c++ code using if else statements to do the following:

1. If character variable taxCode is ’T’, increase price by adding the taxRate percentage of
price to it.
2. If integer variable opCode has the value 1, read in double values for X and Y and
calculate and print their sum.
3. If integer variable currentNumber is odd, change its value so that it is now 3 times
currentNumber plus 1, otherwise change its value so that it is now half of
currentNumber (rounded down when currentNumber is odd).
4. Assign true to the boolean variable leapYear if the integer variable year is a leap year. (A
leap year is a multiple of 4, and if it is a multiple of 100, it must also be a multiple of
400.)
5. Assign a value to double variable cost depending on the value of integer variable
distance as follows:

Distance Cost
0 through 100 5.00
More than 100 but not more than 500 8.00
More than 500 but less than 1,000 10.00
1,000 or more 12.00

You might also like