C - From The Top (part 3 - Getting the flow)

First of all, the answer to last time's question...

#include <stdio.h>
int main(void)
{
 float time,speed,distance;
 speed=2.98E8;
 speed*=60;
 distance = 150E6;
 distance *=1000;
 time = distance / speed;
 printf("Time = %.2f minutes\nTime = %.2e minutes",time,time);
 return 0;
}

So what's the program doing?

Firstly, initialise three floating point variables and assign the appropriate values to them. These values are then converted to seconds or meters. The time taken is calculated by rearranging the formula given and finally the output given. The %.2f means to print to two decimal places past the point. The screen should give

Time = 8.39 minutes
Time = 8.389261 e +00 minutes

I told you it wasn't that hard! Anyway, let's get on to the business at hand...

Flow

C is a simple language. Everything is either true or false. A false value is zero, anything else is true.

Normally, if you had a line such as

IF a%2 = 0 THEN

it would mean that if there is no remainder when a is the modulus of 2 then do something. In C, this can be written the same way, but it is also possible to make this into a short-hand style. A zero is a false value. Therefore, a line such as

if (a%2)

is quite acceptable. If the condition holds (i.e. no remainder), the next stage is processed. Likewise, if the line read

if (!a%2)

then the next stage would be processed if there were a remainder.

These simplifications make working with C not only simpler to understand and debug but, with the fewer instructions, much faster.

C has three main flow statements and three unconditional flow statements.

Goto

One of the unconditional flow statements is goto. As C has no line numbers, the goto statement has to jump to a defined point which has a colon following it. For example

{
 start : printf("Hello")
 .....
 .....
 if (a == 3) goto start
}

Goto statements should be avoided as they make the task of debugging a potential nightmare.

Continue

The second unconditional is continue. This makes the program jump to the next iteration of the loop, bypassing everything else.

{
 do
  {
    a++;
    printf("%d",a);
    if (a%2) continue;
    printf("Not printed if a/2 <> int a/2");
   }
  while (a<100);
 }

Break

The final unconditional is break. This causes the program to terminate the condition it is currently in.

switch (option)
{
 case 1 : do something
          break;
 case 2 : do something else
          break;
}

The three conditional flow statements are if .. else, do .... while and ?.

IF

There are certain things which have to be remembered with C, namely they do not have the likes of <>, AND or OR statements. In Basic, the test for a comparison (if a equals b) would be

IF a = b THEN.

In C, this has to be

if (a == b)

and there is also no THEN statement.

Unlike Basic, there's no ENDIF statement. The IF statement is assumed to be terminated when the conditions have been met or a BREAK command has been encountered.

The format of the IF ELSE statement is

if (variable <condition> variable)
{
}
else
{
}

For instance, if I wanted to test if a variable was >1 or <0, then act on it, the code may look like this:

if ((a > 1) || (a < 0))
{
 printf("The value is >1 or < 0");
}
else
printf("The value is 0");

Switch case

The SWITCH CASE commands are very similar in operation to the Basic CASE <variable> OF WHEN ENDCASE commands.

The format is:

switch (<variable>)
{
 case <cond_1> : do something
 case <cond_2> : do something
}

In the format given above, the program will test for all conditions; if it finds the correct one (say cond_1), then this is executed. However, as there is no reason to come out of the testing loop, the program will go on to execute all the other conditions. To exit after a condition has been met, the BREAK command is placed after each line.

case <cond_1> : do something
                break;

This will terminate the testing loop.

?

The final conditional test in C is the ?. This is a nice simple method of testing a condition, then doing either (a) or (b). The format is:

<variable> <condition> ? (a) : (b) ;

In other words, it's an IF THEN ELSE statement for C. As part of a piece of code, it would look like this:

a == 2 ? printf("a=2") : printf("a<>2");

in other words:

if a = 2 then print "a=2" ELSE print "a<>2"

These condition statements don't just apply to numbers - they can also apply to text strings. However, I've not enough time to cover text strings in this part - they're next!

Your homework

This time's puzzle is a bit more taxing.

Design and write a program which puts up a simple number menu allowing you to add, subtract, modulus, divide and multiply, obtain the inputs, act on it and then print the result. The numbers should be integers.

The final element of the menu is a quit option. To put this into the code, use the following fragment for the input:

At the start (but after the setting of the variable types) begin with a do {.

At the end, have a close } followed by the line:

while (<var> !=6);

This will loop around unless you have asked to quit. The <var> is just a variable name.

The program must also have an error check to ensure that you are not dividing by zero.


Source: Archive Magazine - 13.3
Publication: Archive Magazine
Contributor: Paul Johnson