3

As the title says, I've written some codes and tested it but for some reason I still can't understand it.

for (…)
  {
    …
  }

versus

for (…)
  …

and

while (…)
  {
    …
  }

versus

while (…)
  …

Also, I've seen that in some cases main works without the int, why does this happen? And, how is the proper way to write main?

Sorry for double question, I have many doubts (specially arrays). Thanks and sorry.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
  • Feel free to ask multiple questions here, but don't group them together. – Shomz Jan 30 '15 at 01:24
  • `main` must always return int.. `int main()` or `int main(int argc, char* argv[])`.. You need to learn about scopes to know the difference between `for (...) { blah; }` and `for (...) blah;` – Brandon Jan 30 '15 at 01:26
  • Sorry Shomz. Also, I'm still not into arguments or pointers (I do know they exist and what they are but not using them yet). –  Jan 30 '15 at 01:27
  • As of the old C89/C90 standard, the `int` could be omitted; the return type was still implicitly taken as `int`. C99 dropped the implicit `int` rule, but not all compilers enforce the newer standards. You can get away with writing `main()` with some compilers, but you should always write `int main(void)` instead (or `int main(int argc, char *argv[])`). – Keith Thompson Jan 30 '15 at 01:28
  • No need to be sorry. I see you're new here, so I'm just giving you advice to keep your questions simple and accurate, no need to squeeze multiple questions into one. – Shomz Jan 30 '15 at 01:33
  • I cannot mark it as a duplicate because you have asked multiple question in one but look here for the question about `main`'s return type: https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – 5gon12eder Jan 30 '15 at 03:46

4 Answers4

6

If you leave out the curly braces, the body of the loop is just a single statement. The braces allow you to put multiple statements in the body.

With braces:

while (...) {
    statement1;
    statement2;
    statement3;
}

All 3 statements will be executed repeatedly.

Without braces:

while (...) 
    statement1;
statement2;
statement3;

Only statement1 will be executed repeatedly. The other two statements will be executed once after the loop finishes.

It's a good idea to always use braces. See

Why is it considered a bad practice to omit curly braces?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
3

The syntax of a for or while statement (also an if` statement) is such that it always controls exactly one sub-statement.

A while loop has the form:

while ( expression ) statement

Adding curly braces simply means that you're using a different kind of statement. A block or compound statement consists of a {, followed by zero or more declarations or statements, followed by a }.

Note that the braces are not part of the syntax of the while statement.

So if I write:

while (foo)
    puts("hello");

the while statement controls a single sub-statement, which happens to be a function call. If I write:

while (foo) {
    puts("hello");
    puts("good-bye");
}

the while statement still controls a single sub-statement, but this time it happens to be a compound statement, which itself contains two sub-statements.

The advantage of using a compound statement, as others have pointed out, is that it can help avoid errors when you want to add something to the body of a loop. It's not uncommon to see incorrect code like this:

while (foo)
    puts("hello");
    puts("good-bye");

This is perfectly legal, and most compilers won't complain about it, but the second call is not controlled by the while; it will be executed just once, after the while loop has finished. By consistently using curly braces from the beginning, you can avoid this particular class of bugs.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
2

for/while without {} means that only the next statement will be part of the loop, any statements after that are outside the loop. This leads to many bugs and probably not a good general practice. It's always a good idea to add the {} because then if you ever add more code to your loop you won't cause yourself a headache if you forget to add {} later.

Lex Berezhny
  • 512
  • 3
  • 15
1

Long story short, if you leave out the block C control structures (while, if, for, else...) they will generally iterate on or execute the next statement. This is a bad idea. It turns out you can put a lot of things in place of the block in a while or for loop, most of them are also a bad idea. Let's go to the C99 standard.

6.8.5 Iteration statements Syntax
  iteration-statement:
      while ( expression ) statement
      do statement while ( expression ) ;
      for ( expressionopt ; expressionopt ; expressionopt ) statement 
      for ( declaration expressionopt ; expressionopt ) statement

Notice they all take "statements" not "blocks". What's a "statement"?

6.8 Statements and blocks Syntax
  statement:
      labeled-statement
      compound-statement
      expression-statement
      selection-statement
      iteration-statement
      jump-statement

Long story short, a "compound-statement" is a block, an "expression-statement" is a single expression, and we'll skip the rest right now. This is a while loop with an expression-statement.

while( condition )
    do_something;

This is a while loop with a compound-statement.

while( condition ) {
    do_this;
    and_this;
    and_this;
}

It's a good idea to always use blocks, even for one line. Why? Because you can easily fool yourself into thinking you're looping over statements you're not.

while( condition )
    do_this;
    and_this;
    and_this;

Those last two statements sure look like they're part of the loop, but they're not.

A common mistake is to write something like this:

if( condition )
    do_this;
else
    do_something_else;
    and_also_this;

and_also_this will always be executed.

It is much safer to always include the block.

The main question has already been answered elsewhere.

Schwern
  • 153,029
  • 25
  • 195
  • 336