Skip to content
Related Articles

Related Articles

Improve Article

How a statement is handled inside switch block but outside case

  • Difficulty Level : Medium
  • Last Updated : 27 Sep, 2021
Geek Week

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:

  1. Switch takes an input argument and selects a case.
  2. The expression used in the case should be a constant type.
  3. 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;
}
Output:

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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :