C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 17)
17.
What will be the output of the program?
#include<stdio.h>
int main()
{
int i=4;
switch(i)
{
default:
printf("This is default\n");
case 1:
printf("This is case 1\n");
break;
case 2:
printf("This is case 2\n");
break;
case 3:
printf("This is case 3\n");
}
return 0;
}
Answer: Option
Explanation:
In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".
In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".
Then the break; statement is encountered. Hence the program exits from the switch-case block.
Discussion:
12 comments Page 1 of 2.
Mani said:
7 years ago
Thanks @Adalove.
Anshu said:
9 years ago
You are absolutely right @Adalove.
AdaLove said:
1 decade ago
There are two central ideas to the problem:
1. The default case will be executed only when there is no matching case. The position of the default case does not matter; whether you place it in the end or in between or make it the very first case.
2. If a case is executed and there is no break statement, the subsequent cases are executed without checking the case values util a break is encountered or all cases are executed.
Thus here, default is executed and then case 1 is executed and then execution breaks out of the switch case.
1. The default case will be executed only when there is no matching case. The position of the default case does not matter; whether you place it in the end or in between or make it the very first case.
2. If a case is executed and there is no break statement, the subsequent cases are executed without checking the case values util a break is encountered or all cases are executed.
Thus here, default is executed and then case 1 is executed and then execution breaks out of the switch case.
Ankita said:
1 decade ago
If i=2 then output would be "This case 2".
Snehal said:
1 decade ago
If there is statement like i=2 then what will be the output?
Sheetal said:
1 decade ago
Here if i is initialized with any 1, 2 or 3 then we will get output printed as this is case 1, 2 or 3 respectively. i.e. its not seeing the default case and just switching to the case where its satisfying. And if there is any value of i that's not in the case then default as well as case 1 statement thing is printed and then its breaking out!
Biswajit said:
1 decade ago
In switch statement, if any one case is not satisfied then default statement is encountered.
Govind said:
1 decade ago
@Archie.
I don't think that is possible. Because I did run the same program and the output is as it is said to be. And according to the Switch statement rules this is the right output.
Thanks.
I don't think that is possible. Because I did run the same program and the output is as it is said to be. And according to the Switch statement rules this is the right output.
Thanks.
Archie said:
1 decade ago
I ran this program in Ubuntu and I got only the print statement in default as output.
Hari said:
1 decade ago
In default there is no break statement.
When there is no break statement it does not check for any condition (case) but it prints the statement.
When there is no break statement it does not check for any condition (case) but it prints the statement.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers