2.2 if

The general form of a conditional statement in C is as follows:

'if' '('<bexpr>')' <then-stmt> ['else' <else-stmt>]

In this notation, <bexpr> is a placeholder for a boolean expression. We'll talk about that a little later. then-stmt specifies one statement that executes if and only if bexpr evaluates to true (non-zero). The [] notation means an optional part that may occur zero or one time. else-stmt specifies one statment that executes if and only if bexpr evaluates to false (zero).

What if we need more than one statements to execute if and only if <bexpr> is true? We simply need to use a block statement. A block statement is a {} block that may contain any number of statements (including zero!).

As a habit, I always write conditional statements as this:

if (whatwhat)
{
}
else
{
}

This way, I can fill in the block statements later, without worrying about closing the braces, or using the right indentation.



Copyright © 2006-09-25 by Tak Auyeung