0

So I was reading this sumitter's solution on codeforce , here is the link of his/her code https://codeforces.com/contest/1714/submission/166585583

The "for" I asker is in main().I'm wondering what is this "for" syntax called or any keyword for me to look for it because it's different from the for(int i;i < Number;i++) I have learned.

cltstream::read(T);
    for (; T; --T)
Barmar
  • 741,623
  • 53
  • 500
  • 612
Phat
  • 1
  • 1
  • 5
    It's an ordinary `for` loop, it just doesn't need to initialize the variable. – Barmar Aug 02 '22 at 17:39
  • 1
    Each part of the `for` loop header is optional, you can leave it empty if you don't need to do anything there. – Barmar Aug 02 '22 at 17:40
  • 1
    _"it's different from the `for`... I have learned."_ Different how? What do you think makes it different? – Drew Dormann Aug 02 '22 at 17:41
  • `for(;;)` is also legal. It's an infinite loop. You can do al kinds of stuff in each if the parts of the statement, but it's considered bad design if it's not related to the actual loop statement. – JHBonarius Aug 02 '22 at 17:44
  • 1
    It's "different" because you didn't learn the syntax of a for-loop properly. Blame your teacher or your book. – molbdnilo Aug 02 '22 at 17:45
  • 2
    By the way `for(int i;i < Number;i++)` is bad, as `i` is uninitialized – JHBonarius Aug 02 '22 at 17:47
  • 2
    No offense, but the linked code is no good. please don't do it yourself. competitive website don't lead to good code quality, and one should not learn from it. – apple apple Aug 02 '22 at 17:48
  • Behold, the `for` cookie: `for (;;) { ... }` (oh darn it, I was beaten to it!). In essence, the statements of a vanilla-`for` loop are optional. Without a terminating condition in `for (initializer-stmt; terminating-condition; iterate-action)`, the loop will run forever. In the case of your loop, `T` must evaluate to a "truthy" (non-zero) value to continue running, and each iteration it will decrement `T`. – Rogue Aug 02 '22 at 17:51
  • 1
    cppreference has a pretty good description too: [C++ `for` loop](https://en.cppreference.com/w/cpp/language/for) – Ted Lyngmo Aug 02 '22 at 18:03

0 Answers0