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?
10 Answers
+ 4
I'm not getting the idea, show one example so I can understand?
+ 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.
+ 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.
+ 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.
+ 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.
+ 2
JaguR in java u can't use switch case with Boolean
+ 2
U can try int or char JaguR
+ 2
Coder Kitten thank you. Hope that will help me.