HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 6 Statements

if

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Syntax

if (exp)     /* format 1 */
    statement

if (exp)     /* format 2 */
    statement1
else
    statement2

Arguments

exp

Any expression.

statement

Any null statement, simple statement, or compound statement. A statement can itself be another if statement. Remember, a statement ends with a semicolon.

Description

The if statement tests one or more conditions and executes one or more statements according to the outcome of the tests. The if and switch statements are the two conditional branching statements in C.

In the first form, if exp evaluates to true (any nonzero value), C executes statement. If exp is false (evaluates to 0), C falls through to the next line in the program.

In the second form, if exp evaluates to true, C executes statement1, but if exp is false, statement2 is performed.

A statement can be an if or if...else statement.

Example 1

You can test multiple conditions with a command that looks like this:

if (exp1)     /*  multiple conditions */
    statement1
else if (exp2)
    statement2
else if (exp3)
    statement3
.. .
else
   statement N

The important thing to remember is that C executes at most only one statement in the if...else and if...else/if...else constructions. Several expressions may indeed be true, but only the statement associated with the first true expression is executed.

Example 2

Expressions subsequent to the first true expression are not evaluated. For example:

/* determine reason the South lost the American Civil War */
if (less_money)
    printf("It had less money than the North.\n");
else if (fewer_supplies)
    printf("It had fewer supplies than the North.\n");
else if (fewer_soldiers)
    printf("It had fewer soldiers.\n");
else
{
    printf("Its agrarian society couldn't compete with the ");
    printf("North's industrial one.\n");
}

All the expressions in the above code fragment could be evaluated to true, but the run-time system would only get as far as the first line and never even test the remaining expressions.

Using Braces in Compound if Statements

Use curly braces ({ }) in a compound statement to indicate where the statement begins and ends. For example:

if (x > y) {
    temp = x;
    x = y;
    y = temp;
}
else
    /* make next comparison */

Braces also are important when you nest if statements. Since the else portion of the statement is optional, you may not have one for an inner if. However, C associates an else with the closest previous if statement unless you use braces to show that isn't what you want. For example:

if (month == 12) {    /* month = December */
    if (day == 25)
        printf("Today is Christmas.\n");
}
else
    printf("It's not even December.\n");

Without the braces, the else would be associated with the inner if statement, and so the no-December message would be printed for any day in December except December 24. Nothing would be printed if month did not equal 12.

The Dangling else

Nested if statements create the problem of matching each else phrase to the right if statement. This is often called the dangling else problem; the general rule is:

  • An else is always associated with the nearest previous if.

Each if statement, however, can have only one else clause. It is important to format nested if statements correctly to avoid confusion. An else clause should always be at the same indentation level as its associated if. However, don't be misled by indentations that look right even though the syntax is incorrect.

Example

/* Program name is "if.else_example". */
#include <stdio.h>
int main(void)
{
int age, of_age;
char answer;
/* This if statement is an example of the second form (see
* "Description" section). */
printf("\nEnter an age: ");
scanf("%d", &age);
if (age > 17)
printf("You're an adult.\n");
else {
of_age = 18 - age;
printf("You have %d years before you're an adult.\n",
of_age);
} /* end else */
printf("\n");
printf("This part will help you decide whether to jog \
today.\n");
printf("What is the weather like?\n");
printf(" raining = r\n");
printf(" cold = c\n");
printf(" muggy = m\n");
printf(" hot = h\n");
printf(" nice = n\n");
printf("Enter one of the choices: ");
fflush(stdin);
scanf("\n%c", &answer);
/* This if statement is an example of the third form (see
* "Description" section. */
if (answer == 'r')
printf("It's too wet to jog today. Don't bother.\n");
else if (answer == 'c')
printf("You'll freeze if you jog today. Stay indoors.\n");
else if (answer == 'm')
printf("It's no fun to run in high humidity. Skip it.\n");
else if (answer == 'h')
printf("You'll die of the heat if you try to jog today. \
 So don't.\n");
else if (answer == 'n')
printf("You don't have any excuses. You'd better go \
 run.\n");
else
printf("You didn't give a valid answer.\n");
}

If you execute this program, you get the following output:

Enter an age: 15
You have 3 years before you're an adult.

This part will help you decide whether to jog today.
What is the weather like?
      raining = r
      cold = c
      muggy = m
      hot = h
      nice = n
Enter one of the choices: r
It's too wet to jog today. Don't bother.

© Hewlett-Packard Development Company, L.P.