21

I looked at a few questions regarding this for other languages and some suggest using final but that doesn't seem to work with Dart.

I'm passing in arguments so surely the switch statement cannot contain constants only? A switch statement, much like an if statement is asking if it is or not..ie it's unknown so I don't see how a switch statement can be useful if they have to be constants...?

  setCategory(arga, argb) {
    int result;
    switch (true) {
      case (arga >= 0 && arga < 3 && argb < 35):
        result = 16;
        break;
      case (arga >= 0 && arga < 3 && argb >= 35):
        result = 15;
        break;
        etc

It's returning the error Case expressions must be constant regarding the values arga and argb in the case expressions. What's the best way to remedy this or do I have to use an if statement?

Hasen
  • 11,710
  • 23
  • 77
  • 135
  • I think you should use if-else for such cases, you also can try to optimise your problem using math (some cases can be simplified or nested) – Ammar Hussein Jul 18 '19 at 06:00
  • There are many cases - 16 in fact, I learnt that in this kind of situation it's not good to use if-else. If-else is better when there are just a few cases. – Hasen Jul 18 '19 at 06:02
  • Yes, when your cases are constant values, not expressions. – Ammar Hussein Jul 18 '19 at 06:03
  • @Ammar Atef So you can only use ONE expression? – Hasen Jul 18 '19 at 06:04
  • Yes, `switch(the expression goes here){ case CONSTANT: ...` – Ammar Hussein Jul 18 '19 at 06:08
  • 1
    Ok so switch statements are pretty useless in dart. – Hasen Jul 18 '19 at 06:20
  • @Hasen Traditionally `switch` cases must be constants in most C-based languages (C, C++, Java, ...). The are not useless; they offer an optimization opportunity (using a jumptable instead of a sequence of branches), they offer a debugging opportunity (in some cases, the compiler can warn if there are unhandled cases), and they can provide clearer code structure. Also see [Is there any significant difference between using if/else and switch-case in C#?](https://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case-in-c) – jamesdlin Jul 18 '19 at 15:48
  • Additionally, I would actually argue that the way you want to use arbitrary expressions as `switch` cases would make them useless since they then would offer very little benefit over `if`-`else`. – jamesdlin Jul 18 '19 at 15:50

4 Answers4

17

The switch case expressions must be constants for sure.

You have to use if/then chains to do multiple tests based on non-constant values.

You cannot use arguments from the surrounding function in a switch case. What you are trying to do here is not supported by the Dart switch statement.

The Dart switch statement is deliberately kept very simple, so that a compiler can know all the possible cases at compile-time. That's why they must be compile-time constants.

Switch statements are still useful for some kinds of switching, like on enums:

enum Nonse { foo, bar, baz; }

String fooText(Nonse non) {
  switch (non) {
    case Nonse.foo: return "foo";
    case Nonse.bar: return "bar";
    case Nonse.baz: return "baz";
  }
  throw ArgumentError.notNull("non");
}

You can also switch over constant string values or integer values.

lrn
  • 64,680
  • 7
  • 105
  • 121
  • Do you know how to work with enum extension too? – Ryde Jan 28 '21 at 04:19
  • Extension methods on an enum type works just like extension methods on any other type. You can't make extensions on individual values, so you'll still have to switch on the `this` value. So, nothing different about that for this question. – lrn Jan 28 '21 at 07:35
6

There are some rules for Switch Case

The default case is optional.

All case expression must be unique.

The case statements can include only constants. It cannot be a variable or an expression.

The data type of the variable and the case expression must match.

There can be any number of case statements within a switch.

you should use 'If Else' statement

Community
  • 1
  • 1
Saeiddjawadi
  • 312
  • 2
  • 13
1

Switch statement requires a constant -> it means already initialized variable with final value

switch (expression) {
  case ONE : {
      statement(s);
  }
  break;
  case TWO: {
      statement(s);
  }
  break;
  default : {
      statement(s);
  }
}
Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
1
for(int x=0; x<1; x++){
if(ing5<8 && toWhatDegreeRand<=10 && toWhatDegreeRand>7){
  toWhatDegreeRand=9;
}
if(ing4<8 && toWhatDegreeRand<=7 && toWhatDegreeRand>5){
  toWhatDegreeRand=6;
}
if(ing3<8 && toWhatDegreeRand<=5 && toWhatDegreeRand>3){
  toWhatDegreeRand=4;
}//....

}

It can be a little useful.

onur k
  • 43
  • 7