15

What does a for loop without any curly braces around it do?

So from what I know, that during an if-statement only the first line of the code is executed. So in a for loop how does it work?

I don't really understand the concept of a loop without the braces and with the braces. I guess an explanation with a piece of code would help. This is in C by the way. Here's a code I've been looking at as a reference.

int main(int argc, char* argv[])
{
  int i;
  int count = 0;
  for (i = 0; i < 5; i++)
    count++;
    printf("The value of count is: %d\n", count);

  return 0;
}

In this case, I see that there is no curly braces, so I am assuming that it will just keep iterating the first statement until i < 5 and once i is not less than 5 it doesn't do anything, but when I tested the code I get that it also ends up printing the printf statement. I thought that a loop without curly braces executed only the first line of code? Or am I missing something here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tenza
  • 586
  • 1
  • 3
  • 9
  • 1
    I know there is a duplicate of this, just can't find it... – jpw Oct 09 '14 at 23:28
  • 4
    What happens when you compile and run it? What happens if you then add an opening curly brace after the `for` and a closing one after the `printf`? It seems that about 30 seconds of effort would answer this for you, and it would be a lot quicker (and more educational) than posting here and waiting for an answer. There is absolutely nothing dangerous in that code that prevents you from making a little effort yourself. – Ken White Oct 09 '14 at 23:28
  • 1
    @jpw I already tried a search before I asked the question but I couldn't find anything like what I was trying to ask – Tenza Oct 09 '14 at 23:38
  • 2
    @KenWhite I actually did try this already and got an output which had the value of count at 5 and printed it, but I wanted to confirm if this always applied and how it worked – Tenza Oct 09 '14 at 23:40
  • 1
    Possible duplicate of [When can I omit curly braces in C?](https://stackoverflow.com/questions/14901919/when-can-i-omit-curly-braces-in-c) – zett42 Oct 13 '18 at 11:05
  • There is almost certainly a duplicate from 2008 (canonical). – Peter Mortensen May 27 '22 at 20:02
  • Related (not duplicate, but probably not canonical either): [Dangling *else* problem](https://stackoverflow.com/questions/11734604/) – Peter Mortensen May 27 '22 at 20:45

3 Answers3

20

Without curly braces, only the first statement following the loop definition is considered to belong to the loop body.

Notice that in your example, printf is only called once. Though its indentation matches the previous line, that's a red herring – C doesn't care. What it says to me is that whoever wrote the code probably forgot the braces, and intended the printf statement to be part of the loop body.

The only time I would leave out the curly braces is when writing a one-line if statement:

if (condition) statement;
do_something_else();

Here, there's no indentation to introduce ambiguity about whether the statement on the second line is actually supposed to belong to the body of the if. You would likely be more confident when reading this that it's working as intended.

Air
  • 8,274
  • 2
  • 53
  • 88
  • 1
    So everything else would be considered outside of the body like @gmarintes said? And also does this apply for all loops and also if-statements? – Tenza Oct 09 '14 at 23:32
  • Yes, it applies generally to loops and conditional statements. There are lots of discussions on Google; [here's](http://bytes.com/topic/c/answers/129149-braces-needed) a decent one. – Air Oct 09 '14 at 23:39
  • Note that a one-line `do ... while` is awkward as it requires a semicolon after both the body statement and the condition. – Air Oct 09 '14 at 23:43
3

If the for-loop does not have braces, it will execute the next statement. The syntax is essentially

for (<initialization>;<condition>;<increment>) <statement>;

The "statement" part can be anything. It could be a simple count++; or it could be an 'if'/'if-else' statement, it could even be another for-loop without the braces!

So in this case, the code is similar to:

for (i = 0; i < 5; i++) {
    count++;
}
printf("The value of count is: %d\n", count);
gmarintes
  • 1,288
  • 12
  • 16
  • I'm not sure I understand what you mean by it will execute the statements until the next ';'? So is this similar to saying that if there is a condition in that first line of a loop it will keep iterating through it until it has completed and then finally go through the rest of the lines? – Tenza Oct 09 '14 at 23:30
  • Yes. The syntax is essentially for (;;) ; The part can be anything. If could be a simple count++; or it could be an 'if'/'if-else' statement, it could even be another for-loop without the braces! – gmarintes Oct 09 '14 at 23:33
  • Alright thank you for the help, that cleared things up for me! – Tenza Oct 09 '14 at 23:34
  • "Statements until the next ';'" is meaningless. There can only be one statement. – user207421 Oct 10 '14 at 00:10
1

I would like to add to this that I ran into some code that abused this to do nested for loops.

for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
for(int k=0; k<10; k++)
  doSomething(i, j, k);

In this case each for loop only has one statement (the following for loop or function) after it, and so it doesn't need braces.

heplat
  • 100
  • 2