Home > database >  what happens with if == statement with no arguments?
what happens with if == statement with no arguments?

Time:08-14

With

if (i==1) {something} - program will execute something on true

what happens with

if (i==1) - will it break or do nothing on false?

I've seen program with

if (id == 0x11) //OK

with no {} and don't know if it does something. I think it should use next line as its "insides" but it wouldn't change anything

CodePudding user response:

The next statement will be used as the if block. Opening brace does not necessarily be on the same line with the if, and a single statement if does not need to use opening and closing braces. All of the following are equivalent:

if (expression) // OK
{
    statement1;
}

if (expression) { statement1;}

if (expression)
    statement1;

CodePudding user response:

The syntax is (C11 6.8.4)

if (<condition>) <statement>

where <condition> is some expression with a value compatible to int
and
<statement> is a regular statement or a block statement.

if (foo == 42) return 4; // regular statement ends with semicolon
if (bar == foo) { puts("nope!"); exit(EXIT_FAILURE); } // block statement

And, remember that whitespace is not significant in C text source

if (a == b)

b = 0;

is equal to

if (a == b) b = 0;

CodePudding user response:

If condition was false statements with in if condition were not executed..

  •  Tags:  
  • c
  • Related