Home > Enterprise >  c : What does 'while (cin >> variable, variable)' exactly do?
c : What does 'while (cin >> variable, variable)' exactly do?

Time:02-02

So here's that piece of code, very basic but i just can't find any similar questions that have two variables in the while loop with cin in it.

void Perm(int start, int end, int a[]) {
  //some recursion code
}
int main() {
   int i, n, a[10];
   while (cin >> n, n) {
      for (i = 0; i < n; i  )
      {
         a[i] = i   1;
      }
      Perm(0, n, a);
   }
   return 0;
}

Can't figure out what does while (cin >> n, n) part do, and when will it stops. It looks like when I run the code, the input is just required once..

CodePudding user response:

The comma operator performs the expression to the left of the comma and discards the result then performs te expression on the right and offers up the result for testing. So, in English

while (cin >> n, n) 

says, read from Standard In into integer n and discard whether or not the read was successful, then test the value of n. If n is not zero, enter the loop.

With a modern compiler the point is sort of moot, but I believe it would read better as

while (cin >> n && n)

or in English, if we successfully read from standard in into integer n and n is not zero, enter the loop. It conveys the intent better, but it's moot because if the read fails, as of C 11 n will be set to zero and still exit the loop. Before C 11 you probably got an infinite loop testing the last good, non-zero value of n over and over forever.

As explored in the comments below the point seems to be not so moot. It appears that under some conditions, no data given in this case, the value is not being zeroed. More Standard-reading required here, but it looks as though

while (cin >> n && n)

or similar is required to catch all of the cases.

CodePudding user response:

In the condition of the while statement

while (cin >> n, n) {

there us used the comma operator.

That is the expression consists from two subexpressions, cin >> n and n, that are executed sequentially. The result of the condition is the value of the second subexpression that is implicitly converted to the type bool.

From the C 17 Standard (8.19 Comma operator)

  1. ... A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded-value expression (Clause 8). Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. The type and value of the result are the type and value of the right operand;...

The while loop can be equivalently rewritten like

while (cin >> n, n != 0 ) {

It would be more safer to write the condition of the while statement like

while (cin >> n, n != 0 && n <= 10 ) {

Instead of the comma operator it would be better to use the logical AND operator like for example

while (cin >> n && n != 0 && n <= 10 ) {
  • Related