Understanding and Using the Switch Statement in C++ - Prof. Khan, Schemes and Mind Maps of Data Communication Systems and Computer Networks

A comprehensive guide on the switch statement in c++ programming language. It explains the syntax, how it works, and provides a flowchart for better understanding. The document also includes an example of a simple calculator built using the switch statement. It is a valuable resource for students and developers looking to master the switch statement in c++.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 04/05/2024

WAJID1993
WAJID1993 🇵🇰

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 12: Switch Statement
Lab Objectives:
Switch Statement
How does the switch statement work?
How does the switch statement work?
Software/Tools:
PC
Dev-C++
C++ Switch…Case Statement
The switch statement allows us to execute a block of code among many alternatives. You can do
the same thing with the if...else statement. However, the syntax of the switch statement is much
easier to read and write.
Syntax
switch (expression) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;
case constant2:
// code to be executed if
// expression is equal to constant2;
break;
pf3
pf4
pf5
pf8

Partial preview of the text

Download Understanding and Using the Switch Statement in C++ - Prof. Khan and more Schemes and Mind Maps Data Communication Systems and Computer Networks in PDF only on Docsity!

Lab 12: Switch Statement

Lab Objectives:

 Switch Statement  How does the switch statement work?  How does the switch statement work?

Software/Tools:

 PC

 Dev-C++

C++ Switch…Case Statement

The switch statement allows us to execute a block of code among many alternatives. You can do the same thing with the if...else statement. However, the syntax of the switch statement is much easier to read and write. Syntax switch (expression) { case constant1: // code to be executed if // expression is equal to constant1; break; case constant2: // code to be executed if // expression is equal to constant2; break;

default: // code to be executed if // expression doesn't match any constant }

How does the switch statement work?

The expression is evaluated once and compared with the values of each case label.  If there is a match, the corresponding code after the matching label is executed. For example, if the value of the variable is equal to constant2, the code after case constant2: is executed until the break statement is encountered.  If there is no match, the code after default: is executed. Note : We can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.

Example: Create a Calculator using the switch Statement // Program to build a simple calculator using switch Statement #include using namespace std; int main() { char oper; float num1, num2; cout << "Enter an operator (+, - , , /): "; cin >> oper; cout << "Enter two numbers: " << endl; cin >> num1 >> num2; switch (oper) { case '+': cout << num1 << " + " << num2 << " = " << num1 + num2; break; case '-': cout << num1 << " - " << num2 << " = " << num1 - num2; break; case '': cout << num1 << " * " << num2 << " = " << num1 * num2; break; case '/': cout << num1 << " / " << num2 << " = " << num1 / num2; break; default: // operator is doesn't match any case constant (+, - , *, /)

cout << "Error! The operator is not correct"; break; } return 0; } Output 1 Enter an operator (+, - , *, /): + Enter two numbers:

2.3 + 4.5 = 6. Output 2 Enter an operator (+, - , *, /): - Enter two numbers:

2.3 - 4.5 = - 2. Output 3 Enter an operator (+, - , *, /): * Enter two numbers:

2.3 * 4.5 = 10.

Lab Tasks:

  1. Write a program which takes two integer values as user input and then check if both are equal and tells user the result. If not equal, then it should also display the maximum value as output. Expected Output:
  2. Using Switch statement in C++, write a program that input a character from the user that find out whether it is vowel or consonant number. Expected Output:
  3. Write a program which asks user to input percentage and then it should display the grade using following criteria. Use switch statement.

Percentage >= 95% : Grade A+ Percentage >= 90% : Grade A Percentage >= 85% : Grade B+ Percentage >= 80% : Grade B Percentage >= 70% : Grade C+ Percentage >= 60% : Grade C Percentage >= 40% : Grade D Percentage < 40% : Grade F Expected Output: