3
...
for (int i = 1; i != 9; ++i)
 std::cout << i << std::endl;
...

The header of a for loop statement consists of three parts: an init-satement, a condition and an expression. In the case above, the init-statement is int i = 1;

Seems it is illegal to include a statement with a comma operator as the init-statement.

...
for ( (int i , cin >> i) ; i != 9; ) // 2 Erros
 std::cout << i << std::endl;
...

For the example above, I've received 2 error warnings

(int i , cin >> i) ; i != 9;) Error: Expected primary-expression before 'int'

(int i , cin >> i) ; i != 9;) Error: i' was not declared in this scope

Can someone please explain to me what's the reason for the bug?

SLN
  • 4,772
  • 2
  • 38
  • 79
  • The problem isn't the comma but the `(` and `)` that cannot be part of a declaration. `int i = 0, j = 0` would be allowed. Try `int i = input()` and do the input in the function. – Bo Persson Feb 24 '18 at 16:17
  • You can [look up what is allowed there](http://eel.is/c++draft/stmt.for) and find out that `(int i , cin >> i)` is not one of the allowed forms. – nwp Feb 24 '18 at 16:18
  • 2
    Comma-separated sequence of declarators does not involve `operator ,`. Just write each statement on its own line: `int i{}; for(cin >> i; ...` – user7860670 Feb 24 '18 at 16:18
  • You can have `int i;` followed by a `for (std::cin >> i; i != 9; )`. The practical use of that construction is up for debate. – Ron Feb 24 '18 at 16:19

5 Answers5

5

Simple: that first statement must be a declaration statement.

You likewise cannot write:

int main()
{
    (int i , cin >> i);
}

There is no "comma operator" there, just a syntax error, because that is not how C++ works.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I got it, declaration is not an expression: https://stackoverflow.com/a/38176469/3004698, declaration needs a semicolon in the end rather than a comma. – SLN Feb 24 '18 at 19:11
1

Remember that in declarators you can declare several variable at once. For example

int a,b,c,d;

After the type (token "int") you should only use identifiers to declare variables. In the init-statement of the for you may use several comma statements, but to separate expressions, for example:

int a=2, b=3;
for (a=b+b, b=-a; a < b; a++){
    //....
}
Eduardo Pascual Aseff
  • 1,149
  • 2
  • 13
  • 26
-1

The comma separator is very allowed. Try this

for (int i = 1,j=2; i != 9; ++i) std::cout << j << std::endl;

What you did wrong was that declared a variable and took user value for it in same statement. It is not valid in general also(not just in init) as they are two separate statements i.e a declarative statement and an input statement. Hence should be separated by a semi-colon. So try

int i,cin>>i;

outside and it should give error too. In the code posted in question comma separator in in play not comma operator.

manas
  • 62
  • 5
-1

I might find a possible reason for the error.

For operators that do not specify evaluation order, it is an error for an expression to refer to and change the same object

As a simple example, the << operator makes no guarantees about when or how its operands are evaluated. As a result, the following output expression is undefined:--138 pp C++ Primer 5th Ed.

 //Example from the book
 int i = 0;
 cout << i << " " << ++i << endl;

For this case, if int i on the left hand side of the comma operator refers to the object then the right hand side expression changes the same object i

SLN
  • 4,772
  • 2
  • 38
  • 79
-1

It seems you mean

for ( int i = ( cin >> i, i ) ; i != 9; )

Here is a demonstrative program

#include <iostream>

int main() 
{
    for ( unsigned int i = ( std::cin >> i, i ); i != 0; --i )
    {
        std::cout << i << ' ';
    }

    std::cout << std::endl;

    return 0;
}

Its output might look like

10
10 9 8 7 6 5 4 3 2 1

As you mentioned correctly the first part of the for loop is init-satement that can be either an expression or a declaration. You may not combine a declaration and an expression in this part of the for statement using the comma operator.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335