In the meantime consider the following program, it does a temperature conversion, but it also introduces one or two new concepts:

  1. our counter does not have to be incremented (decremented) by 1; we can use any value.
  2. we can do calculations within the printf statement.

 

#include

 

main()

{

    int fahr;

 

    for ( fahr = 0 ; fahr <= 300 ; fahr = fahr + 20)

        printf("%4d %6.1f\n" , fahr , (5.0/9.0)*(fahr-32));

 

}

 

[program]

and here's another one for you to look at:

 

#include

 

main()

{

    int lower , upper , step;

    float fahr , celsius;

 

    lower = 0  ;

    upper = 300;

    step  = 20 ;

 

    fahr  = lower;

 

    while ( fahr <= upper ) {

                             celsius = (5.0 / 9.0) * (fahr - 32.0);

                             printf("%4.0f %6.1f\n" , fahr , celsius);

                             fahr = fahr + step;

                            }

}


 

 

Conditional Execution


Objectives

Having read this section you should be able to:

  1. Program control with if , if-else and switch structures
  2. have a better idea of what C understands as true and false.

Program Control

It is time to turn our attention to a different problem - conditional execution. We often need to be able to choose which set of instructions are obeyed according to a condition. For example, if you're keeping a total and you need to display the message 'OK' if the value is greater than zero you would need to write something like:

if (total>O) printf("OK");

This is perfectly reasonable English, if somewhat terse, but it is also perfectly good C. The if statement allows you to evaluate a > condition and only carry out the statement, or compound statement, that follows if the condition is true. In other words the printf will only be obeyed if the condition total > O is true.

If the condition is false then the program continues with the next instruction. In general the if statement is of the following form:

if (condition) statement;

and of course the statement can be a compound statement.

Here's an example program using two if statements:

 

 

#include

 

main()

{

    int a , b;

 

    do {

 

        printf("\nEnter first number: ");

        scanf("%d" , &a);

 

        printf("\nEnter second number: ");

        scanf("%d" , &b);

 

        if (a

        if (b

 

       } while (a < 999);

}

 

[program]

Here's another program using an if keyword and a compound statement or a block:

 

#include

 

main()

{

    int a , b;

 

    do {

 

        printf("\nEnter first number: ");

        scanf("%d" , &a);

 

        printf("\nEnter second number: ");

        scanf("%d" , &b);

 

        if (a

                  printf("\n\nFirst number is less than second\n");

                  printf("Their difference is : %d\n" , b-a);

                  printf("\n");

                 }

 

        printf("\n");

 

       } while (a < 999);

}

 

 

[program]

The if statement lets you execute or skip an instruction depending on the value of the condition. Another possibility is that you might want to select one of two possible statements - one to be obeyed when the condition is true and one to be obeyed when the condition is false. You can do this using the

if (condition) statement1;

else statement2;

form of the if statement.

In this case statement1 is carried out if the condition is true and statement2 if the condition is false.

Notice that it is certain that one of the two statements will be obeyed because the condition has to be either true or false! You may be puzzled by the semicolon at the end of the if part of the statement. The if (condition) statement1 part is one statement and the else statement2 part behaves like a second separate statement, so there has to be semi-colon terminating the first statement.


Logical Expressions

So far we have assumed that the way to write the conditions used in loops and if statements is so obvious that we don't need to look more closely. In fact there are a number of deviations from what you might expect. To compare two values you can use the standard symbols:

> 

(greater than)

< 

(less than)

>=

(for greater than or equal to )

<=

(for less than or equal to)

==

(to test for equality)

The reason for using two equal signs for equality is that the single equals sign always means store a value in a variable - i.e. it is the assignment operator. This causes beginners lots of problems because they tend to write:

if (a = 10) instead of if (a == 10)

The situation is made worse by the fact that the statement if (a = 10) is legal and causes no compiler error messages! It may even appear to work at first