N
N
newuser88882020-05-10 15:18:11
C++ / C#
newuser8888, 2020-05-10 15:18:11

Problems with understanding the switch construction, why is default not executed in the next iteration without break?

The next iteration default does not work, but it works with break. I use 2*2.
In general, I’ll formulate the question even more simply: if there is a break, the default is performed in the next iteration, without a break, the default is not performed in the next iteration. How does a brick change whether the code is executed or not? It just breaks the code. Why does it affect behavior since I'm in an infinite while(true) loop.

double term() {
  double left = primary();
  Token t = ts.get(); // get the next token from token stream

    while (true) {
    switch (t.kind) {
    case '*':
    cout << "I'm inside! '*'\n";
      left *= primary();
      t = ts.get();
     // break был здесь, но без брика не работает, но ведь дефолт в некст итерации все равно выполняется
    // а если так - то какая разница, есть break или нет, такие дела, как понимать?
    case '/': {
      double d = primary();
      if (d == 0)
        error("divide by zero");
      left /= d;
      t = ts.get();
      break;
    }
    default:
      cout << "I'm inside! 'default'\n";  // (Я должен был быть тут, абсурд?)
      ts.putback(t); // put t back into the token stream
      return left;
    }
  }
}


In general, I did not know that the following case if there was no break are executed without checking. I'm sorry!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question