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 2 of 3.
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;
}
}
int fun(int choice)
{
switch(choice){
case 1 :
return 1;
case 2 :
return 2;
default
return 3;
}
}
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.
I think we should consider this.
Swati said:
9 years ago
I think break statement is a must in the switch for proper output.
Sharda said:
1 decade ago
How can it b optional? please let me know.
Qamar said:
1 decade ago
Its optional in the sense that it is allowed in the language to skip break statement and compiler will not raise an error.
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.
In case if one needs to exit from the current case break statement is compulsory if not it is optional.
K.lalitha said:
1 decade ago
In some cases it is optional and in some cases it is compulsory. If we use break statement where the condition is true then the next conditions will not execute.
Rahma said:
1 decade ago
I think if we do not use a break statement the code will be executed and compiler not gives error but the output will never be understandable by the user.
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.
Harikanth said:
1 decade ago
I did not get proper explanation can you suggest me in better way.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers