in Programming in C
8,813 views
0 votes
0 votes
#include<stdio.h>
int main()
{
    int a = 5;
    switch(a)
    {
        default:
            a = 4;
        case 6:
            a--;
        case 5:
            a = a+1;
        case 1:
            a = a-1;
    }
    printf("%d \n",a);
    return 0;
}

(a) 5 (b) 4 (c) 3 (d) None of these

in Programming in C
8.8k views

1 comment

There is no break statement, so first a = a + 1 is executed, then a = a-1 is executed.
0
0

2 Answers

3 votes
3 votes
Best answer
Answer 5

since case 5 is executed a=6 then as their is no break case 1 also executes so a=5 again

So a=5 is printed since case 5 is satisfied default doesn't execute
selected by

1 comment

Here after case 5 , case 1 executed... So it means when a break statement is not given in any case  then after that case all other cases also executes.. It doesn't matter condition is satisfied or not with  switch or not?... Please clear my doubt ??
0
0
0 votes
0 votes

the output of the given programm: 

They have not mention the break for any of the case so the default case also not execute ,If we put break in case 5 the answer will be "6".

for refrence check thie link: http://codepad.org/nQifZJsR

edited by

Related questions