How a statement is handled inside switch block but outside case
Last Updated :
27 Sep, 2021
Switch case statements are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. It is a control statement that allows a value to change control of execution.
Syntax:
C++
// Syntax for Switch Case
switch (n) {
// code to be executed if n = 1
case 1:
break;
// code to be executed if n = 2
case 2:
break;
// code to be executed if n
// doesn't match any cases
default:
}
Key Points:
- Switch takes an input argument and selects a case.
- The expression used in the case should be a constant type.
- The statement outside any case will NOT be executed.
This article focuses on the third statement 'The statement outside any case will NOT be executed'.
Example 1: Predict the output of the following program:
C
// C program to illustrate the switch statement
#include <stdio.h>
// Driver Code
int main()
{
switch (1) {
int test = 10;
printf("dead code\n");
case 1:
printf("%d\n", test);
}
return 0;
}
Explanation: The garbage value is obtained as output even though the value of the test is supposed to be 10. It can be clearly seen that the print statement before case 1 is ignored (maybe removed during code optimization as part of dead code elimination) so it can be concluded that the previous statement is also not executed. It means the variable test is not declared. is it true? let's see.
Analyzer and Symbol Table: Lexical Analysis is the first phase of the compiler also known as a scanner or analyzer. It converts the High-level input program into a sequence of Tokens. These tokens can be of different types. The Symbol table is a data structure created and maintained by the compiler. Â It stores information about the scope and binding information about names, information about instances of various entities such as variable and function names, classes, objects, etc.
There are many columns in the symbol table but for simplicity, let's take 'variable name' and 'address' fields.
Example 1: Â int x = 5;
Lexical Analyzer:
int - keyword
x - identifier
= - operator
5 - constant
; - special symbol
Symbol Table:
----------------------------------------------
| variable name | address |
----------------------------------------------
| x | 0 |
----------------------------------------------
Example 2:
void main(int a)
{
int b = a;
}
Lexical Analyzer:
void- keyword
main - identifier
() - special symbol
{} - special symbol
int - keyword
a - identifier
b - identifier
= - operator
; - special symbol
Symbol Table-
----------------------------------------------
| variable name | address |
----------------------------------------------
| main | 0 |
----------------------------------------------
| a | 4 |
----------------------------------------------
| b | 8 |
----------------------------------------------
If statements outside Case are not executed, how is it possible to use the variable 'test' without declaring it? Let's look at how this line bypasses the analysis phase of compilation.
Lexical Analyzer: Lexer will parse the whole program normally. This is the phase where the symbol table is created, the variable test is thus recognized as a token and added to the symbol table.
----------------------------------------------
| variable name | address |
----------------------------------------------
| test | 0 |
----------------------------------------------
Syntax Analyzer: Looks at the syntax of the whole program. Structurally our program is perfectly fine. No error till now.
Semantic Analyzer: it Will try to assign meaning to the parse tree. The variable test is assigned as an integer type and has successfully bypassed all analysis phases.
------------------------------------------------------------------
| variable name | address | type |
------------------------------------------------------------------
| test | 0 | int |
------------------------------------------------------------------
Now identifier test has successfully occupied a place in the symbol table. The symbol table is populated with values at runtime, but because at runtime the lines 5 and 6 are not executed, the value for the variable test is not updated. Hence we get some garbage value as output.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and
9 min read