Help with Switch statement

I need help to understand if this works… and if so, why do I always fall through the default?

function cl(words) {
console.log(words);
}

switch (myAge) {

case (myAge >= 20 && myAge < 30 ):
cl(‘In your twenties.’);
break;

case (myAge >= 30 && myAge < 40 ):
cl(‘In your thirties.’);
break;

case (myAge >= 40 && myAge < 50 ):
cl(‘getting old, feller!’);
break;

case (myAge >= 50 && myAge < 60 ):
cl(‘Over the hill, dude!’);
break;

default:
cl(‘Did you enter a number?’);
}

Do you see any variable named myAge in the program?

Yes, myAge is declared prior to cl function. Sorry, should have included

Ok. Do you see anything wrong with your comparisons?

I am scratching my head, been back over comparison statements, symbols… :face_with_diagonal_mouth:

now focusing on the comparisons… :thinking:

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.

1 Like

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… :roll_eyes:
I suppose the if…else statement gets the job done just as well. :face_with_raised_eyebrow:

1 Like

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;
}
1 Like

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… :face_with_diagonal_mouth: 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.

Thanks for the kick in the grey matter, guys. :smile:

4 Likes

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

1 Like

So, @tgrtim, are saying the (true) comparison in a switch will not compile and work ?

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