Header Responsive Ads

Switch Statement C++

Switch statement C++ example program

In a switch statement, the first expression inside the parenthesis is evaluated and its value is compared with different switch cases inside the switch body. If any switch case is matched, its associated block of code is executed. There is a keyword break after each case. So after the execution of the code block, the break keyword terminates the switch statement. In the end, there is a default case and its associated code block is executed when none of the cases is matched. Note that break and default keywords are optional. 

Syntax of the switch statement in C++

switch(expression)

{
Case 0:
Code block
Break;
Case 1:
Code block
Break;
Default:
Code block
}

Below is a switch case c++ program, in which the income tax of employees in a company is calculated as. 

  • If the salary is less than Rs.10000, there is no income tax
  • If salary falls in the range Rs. 10000-20000, then income tax is Rs.1000
  • If salary exceeds the limit of Rs.20000, then income tax will be 5% of salary
What is switch statement in C++
What is switch statement in C++
Output
What is switch statement in C++ output
What is switch statement in C++ output

Post a Comment

Previous Post Next Post

In Article Ads 1 Before Post

In Article Ads 2 After Post