Control Structures, Relational and Logical Operators

Programming is a complex task by its very nature. Many techniques have been developed over the years to try to make programming easier, faster, and less prone to error. One of the most important techniques which has become accepted as standard is structured programming.

Overview

There was a time when computers had very few resources to spare. They had very small amounts of memory and they were slow. For a while, programmers were cheap compared to the cost of new computer hardware. It made sense in that situation for programmers to spend many hours devising techniques for getting programs to run efficiently on these early computers. In those days, eliminating a few bytes of code could make the difference between success and failure. The programs became unmaintainable because of their complexity. Errors were extremely difficult to fix. Sometimes the whole program would have to be rewritten.

The situation has reversed now. Programmers are expensive and computers are cheap. Modern computers have fast processors and access to large amounts of memory. It makes sense in the current situation to sacrifice some of the computer's resources to make programming more efficient and less error-prone. This is the idea behind structured programming.

Structured programs consist of three type of structures:

[Note: These examples use pseudocode, not C]

  1. sequence: instructions execute one after another starting with the first one
    Example: get employee hours
    get employee rate
    grosspay = hours * rate
  2. selection: a test is performed on some data, and, depending on whether the test turns out to be true or false, either one set of instructions execute, or a different set of instructions execute
    Example: if hours<=40 then
         grosspay = hours * rate
    otherwise
         grosspay = 40 * rate + (hours-40) * (rate*1.5)
  3. repetition: a set of instructions is performed over and over until some test indicates that the looping should end
    Example: repeat for each employee
         calculate employee's gross pay

Your programs will consist of these three control structures.

Relational Operators

We covered a handful of mathematical operators earlier in the course. There are a number of new operators we will look at now because they will help greatly with writing selection and repetition control structures. Some will look familiar if you have had basic algebra.

    Relational operators:
      <    less than
      >    greater than
      <=   less than or equal to
      >=   greater than or equal to
      ==   equal to (note that = is the assignment operator)
      !=   not equal to

Let's take a look at some examples of how the relational operators work:

    int a=5, b=7, c=5;
    a <  b;             /* this would evaluate to true  */
    a >  b;             /* this would evaluate to false */
    a <= b;             /* this would evaluate to true  */
    a <= c;             /* this would evaluate to true  */
    b <= a;             /* this would evaluate to false */
    b >= a;             /* this would evaluate to true  */
    a >= b;             /* this would evaluate to false */
    a == b;             /* this would evaluate to false */
    a == c;             /* this would evaluate to true  */

Logical operators

    Logical operators:
      !    Not
      &&   And
      ||   Or

The && operator evaluates to true only when both of its operand are true, otherwise it evaluates to false. The || operator evaluates to false only when both of its arguments are false, otherwise it is true. The ! operator just evaluates to the opposite of whatever its argument is.

Let's take a look at some examples of how the logical operators work:

    int a=5, b=7, c=5;
    !(a < b);              /* false */
    (a < b) && (a > b);    /* false */
    (a < b) || (a > b);    /* true  */
    (a < c) || (a > c);    /* false */
    (b > a) && (b > c);    /* true */

A truth table is often used to demonstrate how the logical operators work. Read the table line by line. The first line after the headings would be read as: if A is true and B is true, then !A is false, A && B is true, and A || B is true.

AB!AA && BA || B
truetruefalsetruetrue
truefalsefalsefalsetrue
falsetruetruefalsetrue
falsefalsetruefalsefalse