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
Discussion:
26 comments Page 3 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.
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)
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!
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!
Arjita said:
7 years ago
Break statement is to stop the case so it is not compulsary we can use something else to end the case.
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;
}
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)
Mebrie said:
6 years ago
If we don't use break in each condition then give output all result print when execute the program.
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.
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers