0% found this document useful (0 votes)
27 views18 pages

01 139232 040 120057109370 03042024 021417pm

The document discusses different decision making structures in C++ like if, if-else and switch statements. It provides background on decision structures and examples of using if, if-else, nested if-else and switch statements. It also includes 5 lab tasks asking students to write programs using these structures and decision making.

Uploaded by

ridasaman47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views18 pages

01 139232 040 120057109370 03042024 021417pm

The document discusses different decision making structures in C++ like if, if-else and switch statements. It provides background on decision structures and examples of using if, if-else, nested if-else and switch statements. It also includes 5 lab tasks asking students to write programs using these structures and decision making.

Uploaded by

ridasaman47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Experiment 2

Decision Structures (If, If-Else, Nested If-Else, Switch)

Objectives

To familiarize students with the if, if-else and switch decision making structures.

Equipment/ Tools Used:

Microsoft Visual Studio 2022

Background:

Decision Structures:
Decision structures are used to add decision making capabilities within a program. Some commonly
used decision structures in various programming languages are if, if-else, if-else-if, nested if-else and
switch statements.
When instructions within a program are executed one after the other sequentially that program is
said to have a linear structure. Decision making after examining all available options is very
important in life as well as in programming. For example, suppose it is the law that all males 18 or
older should register with the selective service. If you are writing a program to send out reminders to
enforce this law, the decision to send the letter should be based on if a person is male and if he is 18
or older.
Statements executed one after the other in the order written are called sequential statements.

All programs in C++ are written in terms of three control structures.

 Sequence Structure: Built into C++. Programs executed sequentially by default.


 Selection Structures: C++ has three types - if, if/else, and switch
 Repetition Structures: C++ has three types - while, do/while, and for

if statements:

if-else statements:
if-else if statements:

What if you just don’t want to calculate the status as pass or fail. You want to calculate the
grades.

Marks >90 : Grade is A+

Marks >80 : Grade is A

Marks >70 : Grade is B

Marks >60 : Grade is C

Marks >50 : Grade is D

Marks <50 : Grade is F

int marks;
cout<<“Enter marks:”;
cin>>marks;
if(marks >
90){ cout<<“A+
Grade”<<endl;
}
else if(marks >80){
cout<<“A Grade”<<endl;
}
else if(marks >
70){ cout<<“B
Grade”<<endl;
}

else if(marks>60){
cout<<“C Grade”<<endl;
}
else if(marks>50){
cout<<“D Grade”<<endl;
}
else{
cout<<“F Grade”<<endl;
Switch Statements:

Switch statement is an alternative way to write if-else statements

switch ( decision variable )


{
case value1 :
// Statements to execute if variable equals value1
break;
case value2:
// Statements to execute if variable equals value2
break;
...
default:
// Statements to execute if variable not equal to any
value
}
Lab Tasks:

Lab Task 1: Write a C program to find maximum between two numbers. Show the code
and Draw its flowchart below as well

Code

#include <iostream>
using namespace std;

int main() {
int num1, num2;

// Input two numbers


cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;

// Check which number is greater


if (num1 > num2) {
cout << "Maximum number is: " << num1 << endl;
} else {
cout << "Maximum number is: " << num2 << endl;
}

return 0;
}

Output
Flow chart

Lab Task 2: Write a program that displays the grade of a student where the marks are input from
the keyboard. The grading pattern is as following:
>= 70 A
>=60 and < 70 B+
>=50 and < 60 B
>=33 and < 50 C
<33 F
A session of the program execution should print following:
Enter student marks: 56
The grade for 56 marks is B. Draw its flowchart below as well as code

Code

#include <iostream>
using namespace std;

int main() {
int marks;

// Input marks from the user


cout << "Enter student marks: ";
cin >> marks;

// Determine the grade based on the marks


char grade;
if (marks >= 70) {
grade = 'A';
} else if (marks >= 60) {
grade = 'B';
} else if (marks >= 50) {
grade = 'B';
} else if (marks >= 33) {
grade = 'C';
} else {
grade = 'F';
}

// Display the grade


cout << "The grade for " << marks << " marks is " << grade << "." <<
endl;

return 0;
}

Output

Flow Chart
Lab Task 3: Using C++ programming, create a Calculator using the switch Statement

Draw its flowchart below and its code as well

Code

#include <iostream>
using namespace std;

int main() {
char op;
double num1, num2, result;

// Input operator and two numbers


cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;

// Perform operation based on the operator


switch(op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if(num2 != 0)
result = num1 / num2;
else {
cout << "Error! Division by zero.";
return 1; // Exiting the program due to error
}
break;
default:
cout << "Invalid operator!";
return 1; // Exiting the program due to error
}

// Display the result


cout << "Result: " << result << endl;
return 0;
}

Output

Flow Chart
Lab Task 3 (b): Write a C++ program to find the greatest number among three numbers.
Numbers should be initialized from user.

Code

#include <iostream>
using namespace std;

int main() {
int num1, num2, num3;

// Input three numbers from the user


cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
// Find the greatest number among the three
int greatest = num1; // Assume num1 is the greatest initially

if (num2 > greatest) {


greatest = num2;
}

if (num3 > greatest) {


greatest = num3;
}

// Display the greatest number


cout << "The greatest number among " << num1 << ", " << num2 <<
", and " << num3 << " is: " << greatest << endl;

return 0;}

Output
Lab Task 4: Write a C++ program that displays the following Menu for the food items available
to take order from customer.

B - Burger

F – French Fries

P – Pizza

S – Sandwich

The Menu should always be visible (hint: use loop). If any item is selected, then user will be
asked the quantity and the total bill will be printed and then main menu will be displayed. Prices
are as follows

Burger = 500 Rs, French Fries = 300 Rs, Pizza = 1200 Rs, Sandwich = 400 Rs.

Code

#include <iostream>
using namespace std;

int main() {
char choice;
int quantity;
double totalBill = 0.0;

// Menu
cout << "Welcome to the Food Ordering System!" << endl;
cout << "Menu:" << endl;
cout << "B - Burger (500 Rs)" << endl;
cout << "F - French Fries (300 Rs)" << endl;
cout << "P - Pizza (1200 Rs)" << endl;
cout << "S - Sandwich (400 Rs)" << endl;
cout << "Q - Quit" << endl;

// Main loop
while (true) {
cout << "Enter your choice: ";
cin >> choice;

if (choice == 'Q' || choice == 'q') {


cout << "Thank you for using our system. Goodbye!";
break;
}

cout << "Enter quantity: ";


cin >> quantity;

// Calculate total bill based on the choice


switch(choice) {
case 'B':
case 'b':
totalBill += 500 * quantity;
break;
case 'F':
case 'f':
totalBill += 300 * quantity;
break;
case 'P':
case 'p':
totalBill += 1200 * quantity;
break;
case 'S':
case 's':
totalBill += 400 * quantity;
break;
default:
cout << "Invalid choice! Please select from the menu." << endl;
continue; // Go to the next iteration of the loop
}

// Display total bill


cout << "Total bill so far: " << totalBill << " Rs" << endl;
}

return 0;
}

Output
Lab Task 5: Write a conclusion about what you have learned during this lab.

In this lab, use of if-else, switch statement, nested if-else is


implemented on several codes and their outputs are taken. Hence,
Codes are implemented.

You might also like