Can I put if() statements inside the switch()? | SoloLearn: Learn to code for FREE!

+4

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

c#

9/14/2020 4:13:59 PM

Kim L. Layao

18 Answers

New Answer

+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 🤔😉

+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 => $"As a child who is {age} years old, be carefree and play as much as you can.", _ when age >= 18 => $"You're legally an adult who is {age} years old.", _ => $"Enjoy your teenage years since you're still {age} years old." } );

+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. 🤣😂

+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 );

+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.

+8

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. 😂🤣

+8

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. 😉👌

+7

I guess you can: https://code.sololearn.com/cn72Qfr1lvu6/?ref=app

+6

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.

+4

Sure, bro

+3

Why do you asking? Just try it and see if this works or not

+3

Yes you can But there won't be any output.I used C++ for this: https://code.sololearn.com/ct8LKcWEegtS/#cpp But there will be message pop up that the if will never be executed but in IDE like codeblocks and eclipse there will not be that message. That means you can but you won't have the output as it will never execute lol xD.

+2

switch statements were specificly made to not have to use “if()” statements as much. so you’re trying to put them together? Smart! 😁😄😃

+1

You don't... Each of these are transfer of control statements, switch is more direct and compact though ...

+1

Switches are designed to reduce no of ifs why you even want to do that😂😂

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

0

if(age < 12)....? I know how to do it but what I mean is how could I insert the ReadLine()...??

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😅