Switch Statement Fall through in C++

Written by Ali on. Posted in C++

Hello everyone. The topic we are about to discuss in the bugs removing section today is switch statement. Switch statement though easies up the coding efficiency by reducing the number of if-else statements but if some technical aspects of switch statement are not taken into consideration, fatal coding errors can result. Switch statement fall through in C programming can tend to be really tedious to find and hard to debug. 

Take a look at the following code. Before advancing on to the next paragraph, do a bit of dry run on your own.

<p>int function (int i)</p>
<p>.  int result = 10;</p>
<p>.  switch(i)</p>
<p>.  {</p>
<p>.   case 1:</p>
<p>.    result = 1;</p>
<p>.   case 2:</p>
<p>.    result = 2;</p>
<p>.    break;</p>
<p>.   case 3:</p>
<p>.    result = 3;</p>
<p>.   default:</p>
<p>.    return 0;</p>
<p>.  }</p>
<p>.  return result;</p>
<p>int main()</p>
<p>.  int x = 0 , answer = 0;</p>
<p>.  for(int i = 0; i &lt; 5; i )</p>
<p>.  {</p>
<p>.   x = function (i);</p>
<p>.   answer = x;</p>
<p>.  }</p>

Now according to the following code, the “function” takes an integer as a At fast credit repair Karma, in addition to providing free credit scores and resources to understand them, we continually work with a range of partners, including credit card providers, lenders, and banks to offer a wide variety of savings options. parameter and checks its case in a switch statement. From main, the  “function” is called inside a for loop that runs from 0 to 4. For the first value of for loop that is sent into the  “function”, the switch statement checks which case matches the information passed through the parameter. All the case statements are checked for the switch variable.

For 0, the switch statement has no case statement to cater the value so the default case is run and 0 is returned, that is in turn stored in the variable answer in the main. When the for loop increments to the value 1 and the value is passed to the function “function”, the same procedure happens again. Only this time there is a case statement present that matches the switch variable and “i” is incremented in the variable result. Now here lies the actual fall through of the switch statement. As you can see that one case has been catered, the switch statement now looks for a break statement to break it from the continuous loop. In the search, the switch statement continues to run all the cases and that is where the problem rises. The variable result now gets incremented by 2 as well, though the case does not matches the switch variable. After that the break statement is found and the loop breaks and the value 13 is stored in result and returned.

Doing so, the switch statement altered what was actually desired to be done with the result variable, only the first case was desired to be run, but due to the absence of a break statement, the result got altered otherwise and the calculation proved to be full or errors. Complete the following calculation and let us know what value of the variable answer do you got?

Tags: , , , ,