Switch Case statement in C++

Introduction

A switch case is a statement that allows the variable to be compared to a set of variables. Each value is referred to as a case, and the variable is tested for each case. It is also a substitute for the If-Else-If Statement.

Syntax of Switch Case:

switch(conditional-expression){
case ‘value1’:
// code
break; // optional
case ‘value2’:
// code
break; // optional
……
default:
code to be executed when all the above cases are not matched;
}

Sample code:

How the code works?

Choices 1: Given the code, if we type choices A, we are now able to test the Grading System . By simply inputting the 1st, 2nd and 3rd grading and to compute for the Average.

Choices 2: Given the code, if we type choices B, we are now able to test the Simple Math Equation. Enter any number for 1st and 2nd number. The output will be the Answers of Addition, Subtraction, Division and Multiplication.

Note: Only the given choices are able, Capitalized A and B. Other than those input, will get the error message.

Leave a Comment

Your email address will not be published. Required fields are marked *