If statements without brackets

c, if-statement

Solution

If there are no brackets on an if/else, the first statement after the if will get executed.

If statement:

if (condition)
    printf("this line will get executed if condition is true");
printf("this line will always get executed");

If/else:

if (condition)
    printf("this line will get executed if condition is true");
else
    printf("this line will get executed if condition is false");
printf("this line will always get executed");

Note: Your code will break if there are multiple commands between an if and its matching else.

if (condition)
    printf("this line will get executed if condition is true");
    printf("this line will always get executed");
else
    printf("this else will break since there is no longer a matching if statement");

Problem

I'm hoping to get some clarification on `if` and `if else` statements that do not have brackets and how to read them. I can read `if else`, and `else if` statements with brackets easily and they make sense to me but these have always confused me, here is a sample question. ``` if (x > 10) if (x > 20) printf("YAY\n"); else printf("TEST\n"); ```

Original source