C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Switch

C++ Switch

shape Description

CPP Switch and Conditional operators are used in Decision Making. It allows variable or expression value to change the control flow of program execution. This is impossible with nested if..else as this results in more complications and the developer gets confused. Hence, CPP Switch statements usage is the best option in such cases. Although most CPP Switch statements can be rewrited with if statements, it tends to be easier for the compiler to optimize a large Switch statement than a long chain of if statements.

shape Syntax

switch(expression) { case value1: //statements; break; case value2: //statements; break; case value3: //statements; break; case value n: //statements; break; default : //statements; break; }
In the above syntax, "break" is the keyword used to end the case statements. First, the expression is compared with case 1, if case 1 meets the criteria, then the corresponding statements gets executed until the break is reached.Otherwise (If it does not satisfy the condition then) switch goes to next case and the process continues. If neither case is satisfied then switch executes statements of "default case".

shape Flow chart

shape Example

[c] #include <iostream> using namespace std; int main() { float n1,n2; char op; cout<< "Select an operator either + or - or * or / \n"; cin >> operator; cout<<"Enter two operands: "; cin >> n1 >> n2; switch(op) { case '+': cout << n1 << " + " << n2 << " = " << n1+n2; break; case '-': cout << n1 << " - " << n2 << " = " << n1-n2; break; case '*': cout<< n1 << " * " << n2 << " = " << n1*n2; break; case '/': cout << n1 << " / " << n2 << " = " <<n1/n2; break; default: printf ("You did not selected among given operators"); break; } return 0; }[/c] Output: [c] Select an operator either + or - or * or / * Enter two operands: 5 2 5 * 2 = 10[/c]

Conditional Operator

shape Description

Conditional operator returns the result based on expression. It can be used instead of "if else" statement.This is the only operator that has three operands.So,it is also known as "Ternary Operator".

shape Syntax

expression1 ? expression2 : expression3
Conditional operator checks if expression1 is true, then the compiler executes expression2, otherwise executes expression3. Here, three operands are used at a time.

shape Flowchart

shape Example

In the below example the integer "a" is assigned a value based on the expression "b<10". Since "a" is true for the first value "20" is assigned to the integer "a". [c] #include <iostream> using namespace std; int main () { int a, b = 5; a = (b < 10) ? 20 : 40; cout << "Value of a is: " << a << endl; return 0; }[/c] Output: [c] Value of a is: 20 [/c]

Summary

shape Key Points

  • CPP Switch case helps to decide when multiple conditions exist in the program
  • Break keyword stops the execution if the condition is true
  • If no condition is true then default block is executed

shape Programming Tips

In CPP Switch Case statement one can check fixed integer value conditions only.