Computer Science - Object Oriented Programming Using C++ - Discussion

Discussion Forum : Object Oriented Programming Using C++ - Section 1 (Q.No. 5)
5.
The use of the break statement in a switch statement is
optional
compulsory
not allowed. It gives an error message
to check an error
None of the above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
26 comments Page 1 of 3.

Shweta said:   2 months ago
When we don't use break; statement the switch function will show all the cases you mentioned below the first one it wouldn't break the program and go out of it.

So break statement is compulsory in that matter.

But if you want all the cases to be print then it's not.

So both compulsory and optional is right because it will still work without a break statement.

Mebrie said:   3 years ago
If we don't use break in each condition then give output all result print when execute the program.

Nandini said:   4 years ago
Break statement in switch case is compulsory. Because without break statement all the cases will be executed.

And you can run the below program to find out why break statement is compulsory in switch statements:

#include <stdio.h>

int main () {

/* local variable definition */
char grade = 'B';

switch(grade) {
case 'A' :
printf("Excellent!\n" );

case 'B' :
case 'C' :
printf("Well done\n" );

case 'D' :
printf("You passed\n" );

case 'F' :
printf("Better try again\n" );

default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade );

return 0;
}

Arjita said:   5 years ago
Break statement is to stop the case so it is not compulsary we can use something else to end the case.

Harika said:   5 years ago
@ALL.

Sometime it is compulsory, !we write switch statements for executing single case at a time, it is properly done by using the break statement. Without using a break statement, all statements are executed at a time so the outcome is not proper!

Al Amin Sarker said:   5 years ago
If we don\'t use break in each condition then give output all result print when execute the program. Break will give a result only One what will fulfil the condition.

Try to Run It bellow -
#include <stdio.h>

int main () {

/* local variable definition */
char grade = 'B';

switch(grade) {
case 'A' :
printf("Excellent!\n" );

case 'B' :
case 'C' :
printf("Well done\n" );

case 'D' :
printf("You passed\n" );

case 'F' :
printf("Better try again\n" );

default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade );

return 0;
}

In Switch - No break is needed in the default case.

Sasi said:   6 years ago
In the syntax also break is given know, so it may be compulsory.

Sandesha said:   6 years ago
Exactly, The Break statement is mandatory in switch concept.

Renuka said:   6 years ago
Use of break statement in a switch statement is compulsory.

Hemkant said:   6 years ago
It's possible to use the switch case in function which returns the value, based upon choice in the switch, you can return from the case in this situation function will be returned without any need of the break.

int fun(int choice)
{

switch(choice){
case 1 :
return 1;
case 2 :
return 2;
default
return 3;
}
}


Post your comments here:

Your comments will be displayed after verification.