int customer_age; const int min_age = 18; // irrelevant code omitted if ((balance < min_bal) && (customer_age > min_age)) { /* same code as above */ }

The "&&" is the logical AND operator. The logical OR, written as "||", is used in the same fashion. Both of these operators can be used to link logical tests.

One common kind of if-statement is used to assign a value to a single variable:

if (a > 2) {b = 1;} else {b = a;}

There is, however, a more convenient syntax for this specific kind of if statement, the question-mark-colon operator. It is used as follows:

<variable> = (<test condition>) ? <first value> : <second value>;

The above if-statement would be written:

b = (a > 2) ? 1 : a;

Switch-statements

Sometimes the course of your program can change depending on one variable's value. In such an instance you could use nested if-statements:

if (val == 1) { /* code here */ } else { if (val == 2) { /* more code */ } else { if (val == 3) { // etc.

You can see that this might get confusing very quickly. In such cases, you should use a switch-statement: