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

Lab Manual 05

This lab manual focuses on teaching students how to use nested if-else statements and switch-case statements in C++. It includes syntax explanations and several programming exercises, such as checking if a character is an alphabet, validating triangle angles, and implementing a simple calculator. Additionally, it provides a grading system based on percentage scores.

Uploaded by

shahzeb iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Manual 05

This lab manual focuses on teaching students how to use nested if-else statements and switch-case statements in C++. It includes syntax explanations and several programming exercises, such as checking if a character is an alphabet, validating triangle angles, and implementing a simple calculator. Additionally, it provides a grading system based on percentage scores.

Uploaded by

shahzeb iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

LAB MANUAL 05

CONDITIONAL STATEMENTS (NESTED IF-ELSE AND SWITCH-


CASE)

Lab Objectives:
At the end of this lab students will know about

 How to use Nested if-else in C++ programs


 How to use Switch Case Statements

If else Statements:
If condition returns true then the statements inside the body of “if” are executed and the statements inside
body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in
“else” are executed.

if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}

Nested If else Statements:


When an if else statement is present inside the body of another “if” or “else” then this is
called nested if else.
Syntax of Nested if else statement:

if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"

Class/Semester:BCS/2 Lab Instructor: Rabbia Mahum


2

Switch Statements:
In computer programming languages, a switch statement is a type of selection control mechanism used to
allow the value of a variable or expression to change the control flow of program execution via a multi-
way branch.

Syntax:

switch (a) {

case 1:

//some statements

break;

case 2:

//some more statements

break;

default:

//even more statements

break;

Question#01

Write a C++ program to check whether a character is alphabet or not.

Solutions:

#include <iostream>
Using namespace std;
int main()
{
char ch;

Class/Semester:BCS/2 Lab Instructor: Rabbia Mahum


3

/* reads a character from user */


cout<<"enter any character: ";
cin>>ch;

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
cout<<"character is an alphabet.";
}
else
{
cout<<"character is not alphabet.";
}

return 0;
}

Question #02

Write a C++ program to input angles of a triangle and check whether triangle is valid or not.

Question #03

Write a C++ program to check whether a character is vowel or consonant.

Question #04

Write a menu driven C++ program for simple calculator using if-else.

Question #05
Write a program to input three numbers and find maximum between all.

Question #06

Write a C++ program that tells the user that the number entered is less than, greater than or equal to 10?

Question #07

Write a C++ program that tells the user that the number entered is even or odd?

Question #08

Write a menu driven C++ program that ask the user to choose the type in which he wants the output?

Either he wants to convert the entered Celsius temperature in to Fahrenheit or Kelvin?

Question #09

According to your grading system mark the user entered percentage as Grade A, B, C, D, F?

Class/Semester:BCS/2 Lab Instructor: Rabbia Mahum


4

Percentage >=90 A grade

Percentage >=80 B grade

Percentage >=70 C grade

Percentage >=60 D grade

Percentage >=40 E grade

Percentage <40 F grade

Class/Semester:BCS/2 Lab Instructor: Rabbia Mahum

You might also like