TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C++

C++ switch statement

C++

Introduction

In this post, we will discuss the C++ switch statement and its flow in a simple C++ program. When a switch statement is executed, its expression is evaluated. If the value of the expression equals the case expression the flow is transferred to the action with the matching case expression.

Syntax

switch(SwitchExpression){
case CaseExpression1:
action1

break;
case CaseExpression2:
action2

break;
case CaseExpression3:
action3

break;
default:
defaultAction
}

 

Flowchart

 

C++ switch Statement

/*************************
* C++ switch statement demo
*************************/

#include <iostream>
using namespace std;

int main()
{
 int operand1;
 int operand2;
 bool isValid=false;
 char mathOperator;
 int expResult;
 cout << "----Welcome to Simple Calculator-----" << endl;
 cout << "Please enter expression : " << endl;
 cout << "Example:" << endl;
 cout << "2" << endl;
 cout << "+" << endl;
 cout << "3" << endl;
 cout << "--------------------------------------" << endl;
 cin >> operand1;
 cin >> mathOperator;
 cin >> operand2;

 switch(mathOperator)
 {
 case '+':
 isValid=true;
 expResult = operand1 + operand2 ;
 break;
 case '-':
 isValid=true;
 expResult = operand1 - operand2 ;
 break;
 case '*':
 isValid=true;
 expResult = operand1 * operand2 ;
 break;
 case '/':
 isValid=true;
 if(operand2 != 0)
 expResult = operand1 / operand2 ;
 else
 cout << "Infinity!." << endl;
 break;
 default:
 isValid=false;
 cout << "Operator not supported" << endl;
 }
 if(isValid)
 {
 cout << "--------------------------------------" << endl;
 cout << "Result=" << endl;
 cout << operand1 << " " << mathOperator << " "
 << operand2 << " = " << expResult << endl;
 cout << "--------------------------------------" << endl;
 }
 return 0;
}

 

Sample Run

—-Welcome to Simple Calculator—–
Please enter expression :
Example:
2
+
3
————————————–
1073
+
35451
————————————–
Result=
1073 + 35451 = 36524
————————————–

Process returned 0 (0x0) execution time : 11.207 s
Press any key to continue.

 

C++ switch statement

 

—

C++ Tutorials

C++ Tutorials on this website:

https://www.testingdocs.com/c-coding-tutorials/

For more information on the current ISO C++ standard

https://isocpp.org/std/the-standard

Related Posts

g not found OpenSuse

C++ /

Install GCC C++ Compiler on OpenSuse

sum of digits c program

C++ /

Sum of Digits of a Number C++ Program

C++ Hello World Program

C++ /

C++ Hello World Program

C++ /

Object-Oriented Programming Language Examples

C++ /

Object-Oriented Programming Features

‹ Generate Random Numbers in C++ program› C++ while statement

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version