Node:if... else..., Next:, Previous:if, Up:Decisions



if... else...

Let's review the basic form of the if... else... statement:

if (condition)
{
  compound statement
}
else
{
  compound statement
}

As with the bare if statement, there is a simplified version of the if... else... statement without code blocks:

if (condition) statement else statement;

When the if... else... is executed, the condition in parentheses is evaluated. If it is true, then the first statement or code block is executed; otherwise, the second statement or code block is executed. This can save unnecessary tests and make a program more efficient:

if (my_num > 0)
{
  printf ("The number is positive.");
}
else
{
  printf ("The number is zero or negative.");
}

It is not necessary to test my_num in the second block because that block is not executed unless my_num is not greater than zero.