is it compulsory to use break after each case? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

is it compulsory to use break after each case?

9th Jul 2016, 4:45 AM
sudhakar sharma
sudhakar sharma - avatar
11 ответов
+ 4
if we dont use break, then all the following cases will get execute.
9th Jul 2016, 6:42 AM
simy
+ 2
No its not compulsory, had it been compulsory compiler would have been desined not to allow multiple cases without break. However care needs to be taken to put break after every cases. There can always be a case when we may not require break after all cases.
9th Jul 2016, 6:14 PM
Shyam Kumar Gupta
+ 1
switch executes everything from the matched case until a break is encountered
9th Jul 2016, 5:00 AM
Ruchir Gupta
+ 1
it is compulsory because you do not use break after every case this will happen suppose you have 3 cases case 1 ...case 3. like that if the user input a choice related to case when then of course as you know it will execute case 1 but remember you have not used break statement so after the execution of first case all the cases below that case 1will automatically be executed so it will generate a kind of logical or syntax error
15th Jul 2016, 4:21 PM
Navneet
0
yes otherwise each n every case will be evaluated
9th Jul 2016, 4:55 AM
Ruchir Gupta
0
yes in switch case we have to use break statement after each n every case
9th Jul 2016, 6:30 PM
Jayanti Dikshit
0
yes otherwise the all the cases following after "selected" case will be executed also you don't have to put break in the default case because it is the last case.
31st Jul 2016, 5:02 AM
Anand Kumar
Anand Kumar - avatar
0
yes break is necessary otherwise it will execute all cases & then print all of them
27th Sep 2016, 6:56 AM
deepak
deepak - avatar
0
yes break is necessary otherwise it will execute all cases &then print all of them
27th Sep 2016, 6:57 AM
deepak
deepak - avatar
- 1
not compulsory at all.One should use #break according to the need.
13th Jul 2016, 6:20 PM
Bikram Keshari Maharana
Bikram Keshari Maharana - avatar
- 1
yes it is compulsary to use break after each case because if we dont do so all the cases below the true case will also be executed
9th Dec 2016, 5:07 PM
Navneet