1

Can someone explain this syntax of using for and if side by side?

int min_dist = huge;
int v = -1;

for (int i = 0; i < N; ++i) if (!done[i]) {
    if (d[i] >= min_dist) continue;
    min_dist = d[i];
    v = i;
} 
7
  • 2
    What do you mean by side by side? Commented Dec 6, 2017 at 20:41
  • 4
    That's just an if statement inside a for loop. What exactly don't you get? Put a newline before the "if" and it'll do exactly the same but be more readable. Add curly braces if you like to make it even more clear. Commented Dec 6, 2017 at 20:42
  • 1
    the if doesnt have to be on the next line, in most cases how much whitespace (including new lines) you put in is up to you. Commented Dec 6, 2017 at 20:43
  • 2
    That's probably not the most intuitive way to write it, but it's just a loop that contains only an if() statement as its body. So the programmer went clever, and omitted the braces of the loop body. Commented Dec 6, 2017 at 20:44
  • 2
    Don't write code like that. Commented Dec 6, 2017 at 20:47

7 Answers 7

9

It's not side by side. It's an if inside a for. The syntax of a for is

for(...)
  statement

And it just so happens that an if is also a statement. So your code is equivalent to this:

for(...)
  if() {
  }

Because white-spaces are pretty much ignored by the compilation process. It doesn't matter if there's a new line or not. It's still a statement. Just like it doesn't matter for the one-liner

if (d[i] >= min_dist) continue;

And if we don't omit those obviously crucial braces:

for(...) {
  if() {
  }
}

We'll be far less likely to be confused in the future.

Sign up to request clarification or add additional context in comments.

1 Comment

And (what I think is relevant to OP); is the same as for(...) if (...) on one line.
3

This is a case of poorly indented and organized code. It is equivalent to:

for (int i = 0; i < N; ++i)
{  
  if (!done[i])
  {
    if (d[i] >= min_dist) continue;
    min_dist = d[i];
    v = i;
  }
} 

Adding the block {} right after the for line makes it easier to read.

You can add another block for the second if statement to make it still easier.

for (int i = 0; i < N; ++i)
{
  if (!done[i])
  {
    if (d[i] >= min_dist)
    {
      continue;
    }

    min_dist = d[i];
    v = i;
  }
} 

You can simplify it to:

for (int i = 0; i < N; ++i)
{
  if (!done[i])
  {
    if (d[i] < min_dist)
    {
      min_dist = d[i];
      v = i;
    }
  }
} 

The two ifs can be combined to make it simpler still.

for (int i = 0; i < N; ++i)
{
  if (!done[i] && d[i] < min_dist)
  {
    min_dist = d[i];
    v = i;
  }
} 

Comments

2

This is exactly the same:

int min_dist = huge;
int v = -1;

for (int i = 0; i < N; ++i){
    if (!done[i]) {
        if (d[i] >= min_dist) continue;
        min_dist = d[i];
        v = i;
    }
}

As an explanation, when you only have one statement inside a for or if, you don't need the brackets.

2 Comments

You don't need them; no. But you should still put them in (IMHO) as they are extremely helpful to the casual reader of your code - who may be a greenhorn not familiar with the details of C++ syntax. Write code for the readers, not for the compiler (the compiler will figure it out on its own if it is valid).
@JesperJuhl I agree with you, brackets really differentiate the scope, but is good know that they're not always necessary as you can come across with other code.
2

The layout may fool you. It's just a for-loop with a single if-statement:

  • for each item indexed by i
  • if item i not done already, then proces item i in if-body
  • else next item (in for-loop)

Comments

1

I always find when looking at code I don't understand the best method is to rewrite it in a format which I find easier to read, in this case:

for (int i = 0; i < N; ++i) {
    if (!done[i]) {
        if (d[i] >= min_dist) {
            continue;
        }
        min_dist = d[i];
        v = i;
    }
}

The fact that the if statement is beside the loop merely means that that is the expression to be resolved as the loop body (note this is always the case, blocks {} are also used). This if then has a body as well, by putting in the omitted blocks explicitly it is easier to see what is going on.

Comments

0

If you intend about the loop without parenthesis, it can be used to execute a single block of statements. In this case the ifstatement groups all other statements in a pair of parenthesis, making them a single block.

Comments

0

The right way to write this code is like this:

for (int i  0; i < N; ++i)
    if (!done[i] && d[i] < min_dist) {
        min_dist = d[i];
        v = i;
    }

Putting the if on the same line as the for just confuses things. As is having two if statements instead of one.

3 Comments

For extra clarity; add braces for the for loop as well.
@JesperJuhl -- for extra confusion, add braces to the for loop as well. <g> Seriously: this is more of a personal style issue. In my code I don't add redundant braces.
I agree that it's a style issue. But my personal experience is that newbies get less confused if you always add the braces. I myself find that unfamiliar code reads easier that way. So I (personally) am of the opinion that you should always just add the braces - required or not. But; it's a matter of taste..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.