8

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];
}
jezzo
  • 182
  • 10
princearthur791
  • 143
  • 1
  • 6

9 Answers9

17

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.

Pete Becker
  • 69,019
  • 6
  • 64
  • 147
12

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.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
6

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?

Community
  • 1
  • 1
Preston Martin
  • 2,173
  • 2
  • 21
  • 36
6

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.

NathanOliver
  • 150,499
  • 26
  • 240
  • 331
1
  1. In case of FOR loop: You don't have to use braces but according to my knowledge you should. First of all it increases code readability, it can save you form useless debugging the code. For future readers Oracle's code conventions clearly says that you should always use braces.

  2. In case of IF statements: For a single statement you don't have to use braces, but if you want to group more than one statement inside the IF block then you have to use curely braces.

I hope that my opinion will be useful for you.

apajak
  • 21
  • 4
1

Lightness Races in Orbit summed it up perfectly - it's just a matter of good practice. A lot of programmers probably don't use brackets in that manner out of sheer laziness (it IS optional, after all).

If you want code that's easy to read, and even more importantly easy to debug, then you should get in the habit of using brackets after EVERY for loop and if statement.

It'll probably get tedious after a while, but like I said - it's just a matter of good practice.

Joe Lawry
  • 11
  • 3
1

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.

0

It's not recommended to go without braces, as illustrated by the responses.

for (int i = 0; i < 10; i++) /*For-loop only has one executable
                              *statement in its scope, the if-statement, 
                              *so doesn't need braces.*/
    if (num[i] < min) /*If-statement contains only one executable
                       *statement its scope, so doesn't need braces*/
        min = num[i]; //One line of code to execute.

Stick with braces, it'll make your life easier.

NonCreature0714
  • 4,390
  • 7
  • 28
  • 50
0

Well. Loop without bracket can easily get messed up, especially when programmers get tired.

But the compiler can understand it.

Example 1: A complete line that ends with ; and also immediately follows a for statement belongs to the scope of the for statement. But the line after it won't. Just try to compile it and run it.

#include <iostream>
int main(void){
    for (int i = 0; i < 10; i++) 
        std::cout << "in scope of for" << std::endl;
    std::cout << "out of scope of for" << std::endl;
}

Example 2: Same for the if statement

#include <iostream>
int main(void){
    if (1 == 1)
        std::cout << "in scope of if" << std::endl;
    std::cout << "out of scope of if" << std::endl;
}

Example 3:

#include <iostream>
int main(void){
    int num[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int min = num[0];
    for (int i = 0; i < 10; i++) 
            if (num[i] < min) //this if belong to the for above
                min = num[i]; //this line belong to `if` above
                              //so this line also belong to the `for`
                              //as the `for` own the `if`
}
rxu
  • 1,111
  • 1
  • 6
  • 24