C language: if() with no else(): using braces fails

时光总嘲笑我的痴心妄想 提交于 2020-01-11 14:33:09

问题


I'm confused on need for braces after IF() expression. When using IF(){...}ELSE{...} I'm used to using braces around both IF and ELSE blocks. But when I use no ELSE block it works with no braces and fails with braces:
works: IF()...
fails: IF(){...}
Example below, this is for a microcontroller

#include "simpletools.h"
int main()
{
  while(1)
  {
    print("button = %d\n", input(3));
    if(input(3) == 1)                      //works if no braces
      high(14);
      pause(50);
      low(14);
      pause(50);
  } //while
}   // main

回答1:


A single statement follows the if. If you need multiple statements, they must be combined into a single compound statement.

if (input(3) == 1)
    high(14);
pause(50);
low(14);
pause(50);

executes both pause functions and low regardless of what input returns. Where you put the newlines does not affect how C parses the code. You need braces to combine the 4 function calls into a single compound statement:

if (input(3) == 1) {
    high(14);
    pause(50);
    low(14);
    pause(50);
}

The absence or presence of an else clause does not change anything, except that

if (input(3) == 1)
    high(14);
pause(50);
else ...

would result in an error, since the else is not joined to any if statement; the if concludes with the single statement high(14), and the pause(50) is a separate statement altogether, so the else is out of place.




回答2:


Both the if and else clauses control only the immediately following statement. Your code, indented correctly, is actually:

#include "simpletools.h"
int main()
{
  while(1)
  {
    print("button = %d\n", input(3));
    if(input(3) == 1) 
      high(14); 
    pause(50);
    low(14);
    pause(50);
  } //while
}   // main

The fact that two statements may be on the same line is irrelevant; the if still only controls a single statement. If you want to control two or more statements, you must wrap them in a "compound statement" by using braces:

#include "simpletools.h"
int main()
{
  while(1)
  {
    print("button = %d\n", input(3));
    if(input(3) == 1) {
      high(14); 
      pause(50);
    } else {
      low(14);
      pause(50);
    }
  } //while
}   // main

Since the else must immediately follow the if's controlled statement, inserting it after your two-statement line without braces won't work.




回答3:


The way if works is if (condition) stmt1;; if you want more than 1 statement to be executed if the condition is true, you need to wrap them up with {}. I suspect when you say your code "works" with no braces, you mean it compiles, but the execution will be quite different than if you wrapped the 4 statements in {}. Recall that C doesn't give a fig about indentation.




回答4:


The identation (number of spaces or newlines) has absolutelly no meaning to the compiler. Without braces, only the very next instruction is covered by the if.

if (a)
    DoSomething(); DoSomethingelse();
    MoreToDo();

Actually means:

if (a)
{
    DoSomething();
}
DoSomethingelse();
MoreToDo();

Tipp: Always start a newline after ";", Improves readability. Some people also suggest to always use the braces, that further reduces errors such as this one.




回答5:


If input(3)==1 then high(14); will be called.

But pause(50);low(14);pause(50); will always be executed despite the return from input(3)



来源:https://stackoverflow.com/questions/21441560/c-language-if-with-no-else-using-braces-fails

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