0

Can I use switch statement to check a condition and executive some n no of cases for which the condition is true?

I just learnt that 'switch' can execute more than 1 cases if 'break' statement is not written after each case. So I wanted to try this by taking a condition for which more than one cases could be true and hence ideally all of them should get executed. I having some trouble getting output, please help me. I have kept a Boolean condition inside switch statement. Does that work?

18th Apr 2021, 2:24 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
10 Respostas
+ 4
I'm not getting the idea, show one example so I can understand?
18th Apr 2021, 2:37 AM
Ipang
+ 4
Your <x> is an int, but you give it a boolean true, and yet the cases evaluates numbers. Can you explain me what exactly are you wanting to do? cause that code isn't running ATM.
18th Apr 2021, 2:44 AM
Ipang
+ 3
Ipang yes example was what I was looking for and hence tried to prepare a code by myself and ended up with this code.
18th Apr 2021, 2:39 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 3
Coder Kitten wow that article changed my perception towards the switch case statements. Without a 'break' statement, 'switch' doesn't really recognise case statement as something which it should check before entering. Its just flowing from one case statement to the other when there's no break statement. Thanks a lot for sharing it my friend.
19th Apr 2021, 2:52 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 2
Ipang sorry two concepts of my program got mixed up and I didn't notice that. I have updated the correct one.. please have a look. The code might look a bit weird, but I am exploring it's features.
18th Apr 2021, 2:53 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
+ 2
JaguR in java u can't use switch case with Boolean
18th Apr 2021, 3:13 AM
PIYUSH
+ 2
U can try int or char JaguR
18th Apr 2021, 3:24 AM
PIYUSH
+ 2
Coder Kitten thank you. Hope that will help me.
18th Apr 2021, 4:02 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar