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

☆樱花仙子☆ 提交于 2019-12-23 12:06:11

问题


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];
}

回答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.




回答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.




回答3:


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.




回答4:


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?




回答5:


  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.




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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`
}


来源:https://stackoverflow.com/questions/39579686/why-i-dont-need-brackets-for-loop-and-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!