How To Use C++ Switch Statement Briefly Explain in Detail
How To Use C++ Switch Statement Briefly Explain in Detail 

How To Use C++ Switch Statement Briefly Explain in Detail

Switch Statement Definition: All programming languages can be used 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 by search.

The Switch statements can be used case check any other Statement function some similar to the if statement used in programming languages like C Language Java and exists in most high-level imperative programming languages such as Java, and in many other types of language, using such keywords as switch case.

Some Others Programming Language Switch Statement 

👉C Switch Statement 

👉Java Switch Statement 

A switch statement helps in testing the equality of a variable against a collection of values. every worth underneath comparison is understood as a case.

See the switch as a multiway branch statement. you'll shift the execution of the program to varied components that supported the worth of the expression. 


Some Important Points of Switch Statement in CPP

1. Switch Statement can be one or n of case values for a switch expression. 

2. Switch case value must be of switch expression type only.
 
3. Switch Statement does not allow variables. A switch case value must be uniquely identified. In case of duplicate value, compile-time error in switch program.

4. Switch Every case statement can have a break statement that can be used in the switch program. When start execution of the program then the cases match in the switch expression. when cases are match executed case match line then compiler break switch Statement.

5. Switch Statement case value can have a default value is optional.

Syntax of Switch Statement 

switch(expression)

{

case '1':

//case one execute ;

break;

case '3':

//case three execute;

break;

default:

//no case match is default case is executed;

}


How To Use Switch Statement in C++?

#include<iostream>

using namespace std;

int main()

{

int x=2;

switch(2)

{

case 1:

cout<<"case one executed";

break;

case 2:

cout<<"case two executed";

break;

case 3:

cout<<"case three executed";

default:

cout<<"value are not match";

}

}

*****OUTPUT*****

case two executed


The higher than parameters area unit is explained below

1. Variable: This is that the variable that comparison is to be created.

2. Case: There area unit several case statements. everyone compares the variable with a distinct worth. 

3. Break This keyword prevents execution from continued to subsequent case statements.

4. Default: This is nonobligatory. It states what ought to be done, the worth of the variable failed to match any case.


Example of switch statement ATM Program 

#include<iostream>


using namespace std;


int main()


{


float amt,creditamt,debitamt;

char ch;


cout<<"Enter initial Amount:";


cin>>amt;


cout<<"Enter\nC For Credit\n D For Debit\nB For Balance\n";


cin>>ch;


switch(ch)


{

case 'c':


cout<<"Enter Credit Amount:";


cin>>creditamt;


amt=amt+creditamt;


cout<<"New Amount:"<<amt;

break;


case 'd':

cout<<"Enter Debit Amount:";

cin>>debitamt;


if(amt>=debitamt)


{


amt=amt-debitamt;


cout<<"New Amount:"<<amt;


}


else


{


cout<<"Insufficient Amount";


}


break;


case 'b':


cout<<"Amount in Your Account:"<<amt;


break;


default:


cout<<"Invalid input!!";


}


}


*****OUTPUT*****


Enter initial Amount:10000


Enter


C For Credit

 

D For Debit


B For Balance

 

D


Enter Debit Amount:4000


New Amount:6000.0