Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning value in while loop condition

I found this piece of code on Wikipedia.

#include <stdio.h>

int main(void)
{
  int c;

  while (c = getchar(), c != EOF && c != 'x')
  {
    switch (c)
      {
      case '\n':
      case '\r':
        printf ("Newline\n");
        break;
      default:
        printf ("%c",c);
      }
  }
  return 0;
}

I'm curious about expression used as condition for while loop:

while (c = getchar(), c != EOF && c != 'x')

It's quite obvious what it does, but I've never seen this construction before. Is this specific to while loop? If not, how does parser/compiler determine which side of comma-separated expression returns boolean value for while loop?

like image 936
Josip Avatar asked Jul 16 '09 08:07

Josip


People also ask

Can you assign a variable in a while loop?

Yes. you can declare a variable inside any loop(includes do while loop.

How do you put conditions in a while loop?

Suppose in a while loop, you have two conditions, and any one needs to be true to proceed to the body, then in that case you can use the || operator between those two conditions, and in case you want both to be true, you can use && operator. By using if statements and logical operators such as &&, 11,!

Does a while loop need a condition?

The while loop loops through a block of code as long as a specified condition is true.

How do you assign a value to a variable in C ++?

type variableName = value; Where type is one of C++ types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign values to the variable.


2 Answers

The comma operator is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value.

It is also a "sequence point", which means that all side effects will be calculated before the next part of the code is executed.

like image 124
Paul Dixon Avatar answered Sep 28 '22 12:09

Paul Dixon


The comma operator is a weird beastie until you get to understand it, and it's not specific to while.

The expression:

exp1, exp2

evaluates exp1 then evaluates exp2 and returns exp2.

You see it frequently, though you may not realize it:

for (i = j = 0; i < 100; i++, j += 2)

You're not actually using the return value from "i++, j += 2" but it's there nonetheless. The comma operator evaluates both bits to modify both i and j.

You can pretty well use it anywhere a normal expression can be used (that comma inside your function calls is not a comma operator, for example) and it's very useful in writing compact source code, if that's what you like. In that way, it's part of the family that allows things like:

while ((c= getchar()) != EOF) {...}
i = j = k = 0;

and so on.

For your specific example:

while (c = getchar(), c != EOF && c != 'x')

the following occurs:

  • c = getchar() is executed fully (the comma operator is a sequence point).
  • c != EOF && c != 'x' is executed.
  • the comma operator throws away the first value (c) and "returns" the second.
  • the while uses that return value to control the loop.
like image 29
paxdiablo Avatar answered Sep 28 '22 10:09

paxdiablo