Can I put if() statements inside the switch()? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Can I put if() statements inside the switch()?

14th Sep 2020, 4:13 PM
Kim Layao
Kim Layao - avatar
17 Answers
+ 5
David Carroll lol. I am not even a C# programmer 😂 and I just answered based on my knowledge of Java and similar languages. This is likely a case of the OP not knowing what he wants or may be he finds your code too advanced 🤔😉
15th Sep 2020, 8:53 AM
Ore
Ore - avatar
+ 10
Kim L. Layao Review line 52 for a similar example using switch expressions: https://code.sololearn.com/ceLOtuD7YlEs/?ref=app In your case it would look something like the following: var age = 0; int.TryParse( Console.ReadLine(), out age ); Console.WriteLine( age switch { _ when age < 13 =>
quot;As a child who is {age} years old, be carefree and play as much as you can.", _ when age >= 18 =>
quot;You're legally an adult who is {age} years old.", _ =>
quot;Enjoy your teenage years since you're still {age} years old." } );
14th Sep 2020, 5:36 PM
David Carroll
David Carroll - avatar
+ 10
Dang... I guess I missed the mark with this question. 🤣😂 I literally show Kim L. Layao how he can place conditionals within switch expressions in C#, which many people are unaware of... and I don't get a like or any acknowledgement from the OP. I then answer a direct followup question from the OP about using ReadLine() for int age. Not only did I not get any acknowledgement for the follow up code I provided, the OP selects a different response as the correct answer. Man... talk about a tough crowd. 🤣😂
15th Sep 2020, 7:20 AM
David Carroll
David Carroll - avatar
+ 9
Kim L. Layao I think I cover that in my code sample for you above. Here is the snippet from above: var age = 0; int.TryParse( Console.ReadLine(), out age );
14th Sep 2020, 10:18 PM
David Carroll
David Carroll - avatar
+ 9
I'm going to test something here. Kim L. Layao - Hello... Can you see this message from me? If so, say hi. If not, I must be blocked by you. 😂🤣
15th Sep 2020, 1:15 PM
David Carroll
David Carroll - avatar
+ 9
Yellow Keys Hmmm... I'm pretty certain switch statements were introduced as an alternative to if-else-if-else patterns checking the same variable for matching values. So, in a way, they are a simple version of if statements. That said, you might be interested in C# conditional switch expressions, which I posted about above. 😉👌
16th Sep 2020, 2:14 AM
David Carroll
David Carroll - avatar
+ 8
Kim L. Layao You do not need switch for that if (age < 12) { // baby } else if (age <= 19) { // teenager } else { // something } Something like that will do.
14th Sep 2020, 5:01 PM
Ore
Ore - avatar
14th Sep 2020, 4:25 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 7
People... ( Sanjay Kamath and Mouhamad DIACK ) please read existing responses before posting answers that might be duplicates. You might also learn about C# conditional switch expressions if you read earlier posts in this thread. -------- For the rest of you in this thread... --- This will be my last post here as I'll be unfollowing this thread. Please mention me if anyone needs me to reply.
16th Sep 2020, 5:09 AM
David Carroll
David Carroll - avatar
+ 4
Sure, bro
15th Sep 2020, 6:50 PM
Go Go Go
Go Go Go - avatar
+ 3
Why do you asking? Just try it and see if this works or not
15th Sep 2020, 10:45 PM
Grzegorz Brzęczyszczykiewicz
Grzegorz Brzęczyszczykiewicz - avatar
+ 2
switch statements were specificly made to not have to use “if()” statements as much. so you’re trying to put them together? Smart! 😁😄😃
16th Sep 2020, 1:54 AM
Yellow Keys
Yellow Keys - avatar
+ 2
You don't... Each of these are transfer of control statements, switch is more direct and compact though ...
16th Sep 2020, 4:47 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Switches are designed to reduce no of ifs why you even want to do that😂😂
16th Sep 2020, 8:36 AM
Ved Thaker
Ved Thaker - avatar
0
I want to create something like example user input their age and it return like you are a baby , a teen ager or something
14th Sep 2020, 4:28 PM
Kim Layao
Kim Layao - avatar
0
if(age < 12)....? I know how to do it but what I mean is how could I insert the ReadLine()...??
14th Sep 2020, 10:04 PM
Kim Layao
Kim Layao - avatar
0
Thank you for the response everyone... My goodness actually, my problem is now solved😂😂😂😂😂😂.... this is my problem, int age = Convert.ToInt32(Console.ReadLine()); .....😂😂😂😂 sorry everyone for asking😅
15th Sep 2020, 9:20 AM
Kim Layao
Kim Layao - avatar