I am trying to understand a source code and i cannot figure out how the line for(;Q.size();)
is meant to work. Could someone please simplify it for me ?
Asked
Active
Viewed 93 times
-1

Alan Birtles
- 32,622
- 4
- 31
- 60

Alina
- 21
-
6It's the same as `while(Q.size())`. – Blaze Mar 02 '20 at 07:51
-
2Or more clearly, `while(!Q.empty())` – MSalters Mar 02 '20 at 08:08
3 Answers
2
A for
statement consists of three parts, separated by semicolons:
- an init-statement
- a condition
- an iteration_expression
A for
loop is equivalent to this code:
{
init_statement
while ( condition ) {
statement
iteration_expression ;
}
}
The init-statement and iteration_expression can be empty, but the semicolons between them are still required.
In your example, for(;Q.size();)
would thus be equivalent to:
{
while ( Q.size() ) {
statement
}
}

Remy Lebeau
- 555,201
- 31
- 458
- 770
1
Look at it this way:
for(<do nothing>;Q.size();<do nothing>) {//do something}
Now read the definition of the for
loop and see that it fits perfectly.
As mentioned by others, essentially this becomes equivalent to while(Q.size())

SomeWittyUsername
- 18,025
- 3
- 42
- 85
0
It's a for
loop which doesn't care about an incrementing index variable. As Blaze pointed out it's equivalent to a while
loop.
for(;Q.size();)
{
// do something while Q is not empty
}
or equivalently
while(Q.size())
{
// do something while Q is not empty
}