11 Answers
New Answerusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main() { int x=1; int num = 1; while(num < 6) { x++ ; Console.WriteLine(x); switch (x){ case x>800: num =+1; break ; } } } }
1/27/2018 3:24:41 AM
Ahmad Akil11 Answers
New AnswerThe problem is, you cannot do so, and you don't need to. You just need an if statement, not switch. Your switch block cannot evaluated a ranged condition. (at least, not here)
https://stackoverflow.com/questions/20147879/switch-case-can-i-use-a-range-instead-of-a-one-number
Just this then: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main() { int x=1, num=1; while(num < 6) { x++; Console.WriteLine(x); if (x>800) num +=1; } } } }
I'm not able to make any sense out of what you just posted. I merely replaced your switch block with an if statement.
link to the code: https://code.sololearn.com/cXe7BXr49hWQ/?ref=app Problem arise at line case x>800: num =+ 1; x>800 is false (false is a value of bool data type), so the line becomes case false: num =+ 1; but x is int data type, so the error is you assign a bool case for an int variable. Kindly read through some notes on switch and operators to understand the code.
thanks now i understand but how to solve these problem, but how to change it from false to int .. or if i can put (if ) inside the case !!
my question was for switch not for while , how can I add if for case as (var) in switch
Why don't you use a simple "if" instead of a whole switch for one case? You can't put an arbitrary boolean expression into a case, thats why it is not working.
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message