Expected primary-expression before ";" token | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Expected primary-expression before ";" token

Error as above Im trying to write for loop that end when y reaches value of 2. What did i wrong? for (;(y == 2);;) { sth sth nevermind }

6th Dec 2016, 10:05 PM
Rafał Bernat
Rafał Bernat - avatar
2 Answers
+ 3
You have an extra semicolon (;) in the 'for' statement. There should only be two semicolons in the 'for'. Without knowing the context, I would say that the last semicolon is your problem and the statement should be: for (; y == 2; ) { sth sth nevermind } If that is your intent, you could use a while statement like this: while (y == 2) { sth sth nevermind }
7th Dec 2016, 1:20 AM
Gordon Garmaise
Gordon Garmaise - avatar
0
It should look like this: for(int y=0; y<=2; y++){ std::cout << y << endl; } Note that this loop will actually be executed three times, since the string value of y was zero, so the output will be: 0 1 2
6th Dec 2016, 10:21 PM
Stefan Scheller
Stefan Scheller - avatar