Why I don't need brackets for loop and if statement

10,567

Solution 1

Both the for and the if have to be followed by a "statement". A "statement" can be a simple statement, like min = num[i];, a more complicated one like if (num[i] < min) min = num[i]; or it can be a compound statement (i.e., zero or more simple statements enclosed in curly braces), like { std::cout << "enter 10 numbers\n"; std::cin >> num[i]; }

Some people think that cluttering simple statements with syntactically redundant curly braces is good style. Others don't.

Solution 2

Because you don't.

They are optional. That is just how it is.

If you don't use braces to group multiple statements into one, then only the first statement following the for or if preamble is considered part of that construct. (But this rule is transitive, so your assignment is part of your if is part of your for!)

It is important to note that indentation has no effect.

Many people believe[citation needed] that you should always use braces, for clarity.

Solution 3

Single statement if and for loops do not need curly braces. Here is a discussion of why curly braces are a good practice even for single statements.

What's the purpose of using braces (i.e. {}) for a single-line if or loop?

Solution 4

A for loop has the syntax

for (init, condition, post) statement;

Now this means that any single statement right after the for(...) will be the body and will be what is ran for the loop.

Then we have the if statement which has the syntax of

if(condition) statement;

So we can see that the whole if

if (num[i] < min) 
    min = num[i];

Is just a single statement which is all you need for the for loop.

The reason we use {} when we have more than one line in a loop or condition is to let us use multi-statement loops/conditions as the whole block is treated as a single statement.

Solution 5

We basically use brackets to keep code clean and as much as i know about coding the compiler is not so smart so if you have multiple statements in a loop or in an if condition then we use brackets (curly braces {}) just to let the compiler know that every statement in the braces are a part of the loop or a part of the if statement.

Share:
10,567
princearthur791
Author by

princearthur791

Updated on July 14, 2022

Comments

  • princearthur791
    princearthur791 almost 2 years

    I don't understand why I don't need brackets in this case

    for (int i = 0; i < 10; i++) 
        if (num[i] < min) 
            min = num[i];
    

    And why I need brackets in this case

    int num[10], min;
    for (int i = 0; i < 10; i++) {
            cout << "enter 10 numbers" << endl;
            cin >> num[i];
    }
    
    • πάντα ῥεῖ
      πάντα ῥεῖ over 7 years
      Better you keep them. The syntax without is just error prone.
    • Jarod42
      Jarod42 over 7 years
      The grammar allows it.
    • Ayxan Haqverdili
      Ayxan Haqverdili over 5 years
      @πάνταῥεῖ but it is used a lot in programming books, Is there an advantage of that syntax anyway?
    • πάντα ῥεῖ
      πάντα ῥεῖ over 5 years
      @Ayxan As mentioned, it's error prone, e.g. if statements are added later on and the maintainer forgets to add the braces.
  • Slava
    Slava over 7 years
    Single line? op1; op2; is that a single line?
  • Lightness Races in Orbit
    Lightness Races in Orbit over 7 years
    FSVO "line", yes.
  • Slava
    Slava over 7 years
    I think you should change your wording, "single line" is misleading
  • Preston Martin
    Preston Martin over 7 years
    What is a better wording than single line? Single line of execution?
  • Lightness Races in Orbit
    Lightness Races in Orbit over 7 years
    iosteram? Talk about getting tired! And this is C++; no need for void in empty parameter lists.
  • Slava
    Slava over 7 years
    Statement. Inexperienced developer may think that putting multiple statements in one line of code (textually) will make them "single line"
  • Preston Martin
    Preston Martin over 7 years
    Fixed. Thank you @Slava!
  • Richard Hodges
    Richard Hodges over 7 years
    Other people think that obfuscation can be fun ;-) ioccc.org
  • Lightness Races in Orbit
    Lightness Races in Orbit over 7 years
    The compiler is extremely smart, but it cannot read your mind.