80-Bus News

  

November–December 1984, Volume 3, Issue 6

Page 32 of 55

Control mechanisms

All the usual controlling mechanisms are available to you these are

if<condition> … else…
for(<loop start> ; <loop end condition> ; <loop step> )…
do … while<condition>
while<condition>…

and the very handy ‘switch’ multiple condition mechanism.

if.. else and do.. while require no explanation but I would just like to run through the for loops and switch statements.

The for loop in the example program went like this:

for(count=0; count <= 100; count++) putw(count, fd);

This translates as set count to 0, WHILE count is less than or equal to 100 call the putw function to output the value of count to the disk file, and at the end of each iteration increment the value of count by 1 (count++).

This is the normal format of any for loop but I don’t see any reason why you could not put fred<=100 instead of count<=100 to make the end condition dependant upon some other variable and give you added flexibility.

The switch statement is a pearl and saves you the agony of entering multiple if conditions. It is ideal for menu driven applications. A typical example may be:

switch(value)                 /* value is a variable */
     {
     case 1:
          function1();
          break;
     case 2:
          function2();
          break;
     case 3:
          function3();
          break;
     default:
          printf("ERROR - illegal option");
          errflag = TRUE;
     }

Tests are done on ‘value’ and if it is found to be 1, 2, or 3 then the appropriate function is called and on return the case structure is exited via the ‘break’ statement. If none of the valid cases are found to be true the

Page 32 of 55