Lets take this as an example. Its what language a programmer is using.
const language = "python"
switch (language) {
case "javascript":
console.log("I use javascript and it is an OOP.")
break
case "python":
console.log("I use Python and it is an OOP.")
break
default:
console.log("I program. I don't know what language.")
}
This program would output I use Python and it is an OOP. Note that it does not use ==, ===, <, > or any other comparison operators.
So, a comparison cannot be used in a switch? I would have to have one (1) case (): for each possible number entered, then, correct?
I originally had an if…else series with the comparisons to boil down the data to an age range. I am guessing that I can simplify the step to just the switch.
I am practicing minimal code… efficiency… clean white space… challenging myself beyond reasonableness…
I suppose the if…else statement gets the job done just as well.
The working basis of the a switch is to match the parameter expression to a case expression, and in the event no match is found, switch to the default.
Matching expressions means also matching data type. If we wish to use a comparison expression in the cases (a boolean) then we have to have a boolean in the parameter.
a = Math.floor(Math.random() * 2 - 1) * Math.floor(Math.random() * 10 + 1)
switch (true) {
case a < 0: y = -1; break;
case a > 0: y = 1; break;
default: y = 0;
}
Yes but you dont need any more function calls. You can chain them. Lets take this as an example. How many years a developer has been programing.
const yearsOfPrograming = 2
switch (yearsOfPrograming) {
case 1:
case 2:
case 3:
case 4:
console.log("You have been programing for less than 5 years")
break
default:
console.log("You have been programming more than 5 years")
}
The program above would output You have been programing for less than 5 years. You can do this because there is not a break statement after each case.
All right, @mtf , @how_to_program, as I have been programming for less than 5 years, currently on day 5, I am still perplexed, sorry.
would case if(var >= # && var < #):
as a case instance work, as it turns it into a true/false scenario?
cheers fellers
nm, I tried the embedded if… No go.
With head hung in shame… yes, I switched the parameter from (myAge) to (true), so it picked which comparison was true as opposed to which one fell into a specified range.
I’m not sure if C ever introduced ranges in switch statements. I think some C++ compilers did though. So you’re searching for something in the language that others thought would be useful . Sadly I don’t think it ever reached any language standards in either C or C++ so even if you found a compiler to do this it wouldn’t be fully portable.
I’ve seen workarounds like using a function to return case values (so the functions returns say 1 for values between 10 and 20, 2 between 20 and 30) but then you have two complex code structures for one task. If you have a web search you might find some decent alternatives.
Only realising now that this isn’t even C based so you can basically ignore my previous comment. Whilst the basic design of switch seems the same run-time expressions seem to be valid case matches so that does allow for this pattern to be used (match against true).