No matter what programming language, in addition to a comprehensive collection of variable types / forms, a robust collection of logic test statements is very important. From those logic primitives, comes looping, branching constructs, which we will see very soon.

 

  1. Open both terminal window & text editor, and navigate to your work subdirectory.
  2. Save the block of code below as 'main.c'.
  3. (copy from 'pgm4' the 'goc' text file, make it executable.)
  4. Compile using "./goc main.c pgm5", then run './pgm5'.
  5. Notice when invoking with "./pgm5 red rose", the two command-line parameters are ignored by the program

 

(right-click to download)

/* 'pgm5' source */

#include <stdio.h>

// we can leave these command line variables present, and NOT use them
int main(int argc, char *argv[]) {
int qnty_ducks;
int qnty_chickens;
float day_temp;
float night_temp;

// initialization of variables
qnty_ducks = 13;   qnty_chickens = 47; // OK to have more than one statement per line
day_temp = 43.7;   night_temp = 22.3;

// show the initialized variables

printf("\n");
printf("qnty_ducks = %d, qnty_chickens = %d\n", qnty_ducks, qnty_chickens);
printf("day_temp = %.1f, night_temp = %.1f\n\n", day_temp, night_temp);

// now evaluate and display conclusions

printf("So, what does the data at hand mean??\n");

if (qnty_chickens > qnty_ducks)   // test for qnty_chickens GREATER THAN qnty_ducks
     printf("Time to make soup for everybody!\n");    // if test TRUE, print this
else
     printf("Can't wait for those ducks to leave...\n");    // if test FALSE, print this

if (night_temp < day_temp)    // test for night_temp LESS THAN day_temp
    printf("The temperatures today seem sensible\n");    // if test TRUE, print this

printf("\n");
return(0);
}

 

By now, we've seen 4 ways to test logic: '==' (is equal?), '!=' (is not equal?) '<' (is the left item less than the right item?) and '>' (is the left item greater than the right item?).