switch in c program

how to solve this?

Shraddha @shraddhagami
28 Jan 2017 11:07 pm

Option A


sonali @sonalirangwani
29 Jan 2017 09:04 am

answer is 16,21

Shraddha @shraddhagami
29 Jan 2017 09:46 am

Aww... I forgot to increment 'i' in for loop

Akanksha Bhardwaj @akanksha13
29 Jan 2017 01:31 am


Sumit Verma @sumitkgp
29 Jan 2017 05:28 am

Option B ??

Remember simple property of switch case: 

1.First of all expression inside switch will be executed, i.g, switch(<expression>).

2. Now the value of this expression will be matched with 'case' blocks. If no match then 'default' block will be chosen.

3. If we will not use 'break' statement then the following 'case' blocks or 'default' block will also get executed untill it encounters first 'break' or end of the 'switch' block. 

Now in this question:

Initially, i=0. 

Case 0, will execute first. It will make i=5. Since there is no 'break' statement, so the following cases will also get executed, making 'i' value to 7,  12 and at the end 16(default case). So in 1st iteration 16 will get printed.

Now in 'for' loop, i= 17, only deafult case will execute , now i=21. So 21 will get printed.

It will terminate the 'for' loop. 

sonali @sonalirangwani
29 Jan 2017 09:03 am

option B is the right answer. 

here in the second iteration after i=17 we directly go for the default case because for first iteration we have met the condition of '3' as you listed above. So, now it will match all the case blocks and it will not find '17' so will go for default case ?  is it true?