C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 15)
15.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=3;
    switch(i)
    {
        case 1:
            printf("Hello\n");
        case 2:
            printf("Hi\n");
        case 3:
            continue;
        default:
            printf("Bye\n");
    }
    return 0;
}
Error: Misplaced continue
Bye
No output
Hello Hi
Answer: Option
Explanation:

The keyword continue cannot be used in switch case. It must be used in for or while or do while loop. If there is any looping statement in switch case then we can use continue.

Discussion:
9 comments Page 1 of 1.

Anusha said:   4 years ago
Why not default condition be executed? please anyone explain.

Mahesh Reddy said:   5 years ago
@Anjali.

The output is DoSecond.

Because inside the switch condition the printf("Do") return 2(width of the inside printf() is 2), why because the width of inside printf() function is 2('D' and 'o' are two characters so it returns 2,note: whitespace also count as a, one character ), so the output is DoSecond.

Anjali said:   6 years ago
#include <stdio.h>
int main()
{
switch (printf("Do"))
{
case 1:
printf("First\n");
break;
case 2:
printf("Second\n");
break;
default:
printf("Default\n");
break;
}
}
Can anyone Explain this with an Output?
(2)

Satish Pawar said:   8 years ago
Thank You for the given explanation.

Ramya said:   1 decade ago
Continue statement can't be used in switch-case syntax.

Avinash raj said:   1 decade ago
Some example of continue:

while(test condition)
{
-----------------
if(--------)
continue;
-----------
-----------
}
Here after continue control direct goes at "while(test condition)".

do
{
------------
if(------)
continue;
-----------
-----------
}while(test condition);
Here after continue control goes at "while(test condition)".

for (initialization; test condition; decrement/increment)
{
--------
if(-----)
continue;
--------
--------
}
Here after continue increment/decrement section executed
before test condition.

Avinash raj said:   1 decade ago
Continue causes the loop to be continued with the next iteration after skipping any statement in between i.e.

"continue" tells to compiler "skip the following statement and continue with next iteration.

Hence when "continue" occur in above question control goes out of switch statement.

Rahul khandekar said:   1 decade ago
Why not default condition is executed? please anyone answer.

Gaurav said:   1 decade ago
The keywords break can be used in switch cases as well as in nested loops however continue is used in loops only.

Post your comments here:

Your comments will be displayed after verification.