-1

I hope you guys all are having a great day!

I have a quick question about using the while loop for competitive programming (we do not know the size of the input, so we have to read until the end of file or 0 value) For this particular program, the program end with 2 values of 0 as "0 0", and the code I saw used this:

while (cin >> r >> n, r || n) { 
     // code
}
  1. My question is about the >>> , r || n <<<< part:

    • Is the while loop as the same meaning as

      while ( (cin >> r >> n) ||  (r || n) )
      
    • can I have some preferences to read more about the multi conditions for the while loop.

Please regard my dump question :( Tks you all for reading this post!

Slava
  • 43,454
  • 1
  • 47
  • 90
NNguyen
  • 113
  • 1
  • 6
  • 1
    How did you get from the comma operator (google that) to the `||` operator? – Ed Heal Mar 23 '16 at 19:15
  • 1
    RTFM http://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator – Slava Mar 23 '16 at 19:19
  • It is not the same meaning as || it means basically, do the left side first, then evaluate the right side and treat that expression as the value of the whole expression (see my answer below) – LawfulEvil Mar 23 '16 at 19:22

1 Answers1

2

Basically.... comma has the lowest precedence and is left-associative.

Given A , B

  1. A is evaluated
  2. The result of A is ignored
  3. B is evaluated
  4. The result of B is returned as the result.

Further Reading : https://stackoverflow.com/a/19198977/3153883

So in your case, cin loads r and n. The return value from that operation is ignored. r or n happens and is the result of the whole while expression. So, a 0 0 will cause the while loop to terminate.

Community
  • 1
  • 1
LawfulEvil
  • 2,267
  • 24
  • 46