Switch Statements in C++ PDF
Document Details
Alvin Herrera
Tags
Summary
This document explains C++ switch statements. It covers the syntax, rules, working, and examples of switch statements in C++. It also includes advantages, disadvantages, and comparisons with if-else-if statements.
Full Transcript
Switch Statements in C++ Course Code: CC 123 (B) Course Title: Introduction to Programming (C++) Prepared by: Alvin Herrera, Instructor 1 What is a Switch Statements? The switch statement in C++ is a flow control statement that is used to execute the different...
Switch Statements in C++ Course Code: CC 123 (B) Course Title: Introduction to Programming (C++) Prepared by: Alvin Herrera, Instructor 1 What is a Switch Statements? The switch statement in C++ is a flow control statement that is used to execute the different blocks of statements based on the value of the given expression. It is an alternative to the long if-else-if ladder which provides an easy way to dispatch execution to different parts of code based on the value of the expression. Syntax of a Switch Statement switch (expression) { case value_1: // statements_1; break; case value_2: // statements_2; break;.......... default: // default_statements; break; } Rules of the Switch Statement There are some rules that we need to follow when using switch statements in C++. They are as follows: The case value must be either int or char type. There can be any number of cases. No duplicate case values are allowed. Each statement of the case can have a break statement. It is optional. The default Statement is also optional. Working of Switch Statement The working of the switch statement in C is as follows: Step 1: The switch expression is evaluated. Step 2: The evaluated value is then matched against the present case values. Step 3A: If the matching case value is found, that case block is executed. Step 3B: If the matching code is not found, then the default case block is executed if present. Step 4A: If the break keyword is present in the block, then program control comes out of the switch statement. Step 4B: If the break keyword is not present, then all the cases after the matching case are executed. Step 5: Statements after the switch statement is executed. 1. Switch expression should result in a constant value If the expression provided in the switch statement does not result in a constant value, it would not be valid. Some valid expressions for switch case will be, // Constant expressions allowed switch(1+2+23) switch(1*2+3%4) // Variable expression are allowed provided // they are assigned with fixed values switch(a*b+c*d) switch(a+b+c) 2. Expression must evaluate only int or char type values. The switch statement can only evaluate the integer or character value. So the switch expression should return the values of type int or char only. 3. Break in switch case The break keyword is used in the switch case to break out of the switch when encountered. It is used at the end of every case block so that when the matching case is executed, the program control comes out of the loop. The break statement is optional. If omitted, all the cases after the matching case will also be executed. Example of switch case without break statement int var = 2; // switch case without break switch (var) { case 1: printf("Case 1 is executed.\n"); case 2: printf("Case 2 is executed.\n"); case 3: printf("Case 3 is executed.\n"); case 4: printf("Case 4 is executed."); } Output of switch case without break statement Case 2 is executed. Case 3 is executed. Case 4 is executed. 4. Default in switch case The default keyword is used to define a default case which will be executed when no case value is matched. It is also an optional statement and the switch case statement runs without problem if it is omitted. 5. No duplicate case values In the C switch statement, duplicate case values are not allowed. All the case values must be unique. 6. Nested Switch Statements In C++, we can nest one switch inside another switch statement without any problem. Though it is avoided most of the time as it makes the program more complex and unreadable. Example switch (expression) { case value_1: // inner switch switch (expression) { case 0: //Inner Statement 0 break; } break;.......... default: // default_statements; break; } 7. Position of the default case does not matter Regardless of its placement, the default case only gets executed if none of the other case conditions are met. So, putting it at the beginning, middle, or end doesn’t change the core logic (unless you’re using a less common technique called fall-through). Example switch (expression) { default: // default_statements; break; case value_1: // statements_1; break; case value_2: // statements_2; break;.......... } Advantages of Switch Statement Easier to debug and maintain for a large number of conditions. Easier to read than if else if. Disadvantages of Switch Statement Switch case can only evaluate int or char type. No support for logical expressions. Have to keep in mind to add a break in every case. Disadvantages of Switch Statement Switch case can only evaluate int or char type. No support for logical expressions. Have to keep in mind to add a break in every case. Switch vs. If-else-if switch if else if It executes the different cases on the basis It executes the different blocks based on of the value of the switch variable. the condition specified. It can only evaluate the int or char type It can evaluate any type of expression. expressions. Faster and easier to read for a large It can get messy when there are lots of number of conditions. conditions. Exercises Create a simple calculator using switch statement Create a program that returns day based on the numeric value.