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.

Al Amin Sarker said:   8 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.
(1)

Nandini said:   6 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;
}
(2)

Shweta said:   3 years 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.
(1)

Wasif said:   1 decade ago
Its not compulsory becouse when we want to prform same task for multiple cases in single itteration at that time there is no need use break statement for each case.

Eg:.

As + and - having same priority & * and / has same priority we can write.

Switch (operator).
Case +:.
Case -:.
Return 1;.
Break;.
Case *:.
Case /:.
Return 2;.
Break;.

Hemkant said:   9 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;
}
}

Maanasi said:   1 decade ago
Including break in action block in order to exit from the switch statement. If you don't use a break statement, the program will just continue into the next case block.

In case if one needs to exit from the current case break statement is compulsory if not it is optional.

Kajal said:   9 years ago
The keyword break is compulsory in switch statement because switch statement is not fulfil without break. The syntax of switch statement contains break so if we are not write the break in switch statement then syntax must be wrong.

I think we should consider this.

Harika said:   8 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!

MRT said:   1 decade ago
I though this is compulsory. because we never use break option then. it execute all statement and if i use default option the it always execut and may be give wrong result ...!

Prachi said:   1 decade ago
I think if we not write keyword after each case then all cases starting from first to last will execute, main thing is compiler not gives error, it is logical error.


Post your comments here:

Your comments will be displayed after verification.