IF
Allen Wyatt tells us in "Writing Your First Computer Program" (IDG Books, 2000) that "The If____Then_____ structure is perhaps the most used of any programming structure" (page 103). If____Then____is a grammatical structure from the vernacular. It is also used in formal logic in much the same way and has its own symbol, generally accepted in philosophy of logic as ->. If P then Q becoms If P -> Q.

The use of the if statement in C gives us a chance to see how logic, language, communication and intelligence are all linked in the use of a computer language for the purpose of writing programs. When we communicate with the machine via a C program using an if statement, we are instructing the machine in how to make a decision. Decision making is considered to be a sign of intelligence so we are imparting artificial intelligence to the machine by teaching it via a language of the machine, ie C. That's not so different from how humans learn, is it? We learn in our language. The machine learns in its language. Learning ability is considered to be another sign of intelligence so here we have another example of AI.

Schildt tells us in the introduction to Chaper 2: "In this chapter you will learn about two of C's most important program control statements: if and for. In general, program control statements control your program's flow of execution. As such, they form the backbone of your programs." (p.43). The expression "control statement" is widely used but Schildt also notes that "The if statement is one of C's selection statements (sometimes called conditional statements)." (p.45). Thus we have alternate terminology.  A&J tell us "The if statement is is one of C's program control statements. Others, such as do and while, are covered on Day 6, 'Basic Program Control'" (p. 67).

A&J have the best text I have yet encountered when it comes to explanation of a computer language using the "GENERAL GRAMMATICAL FORM". The GGF for an if statement is given on page 67 as:

if (expression)
statement;

The first word "if" (without the quotes) is part of the C lexicon. The entire GGF gives us the syntax for this portion of C code.  As Wyatt says (page 9) "(Syntax) is nothing but a fancy way to describe how computer programming instructions or commands should be put together". To explain the syntax, A&J use ( and ) which are standard C symbols and part of the C language but the word "expression" between the brackets  is a Standard English word and when we are writing C, we substitute any valid portion of the C language for "expression". For example, we may substitute in the GGF, the C expression for A = B in English. That translates into A = = B in C. THE GGF GIVES US A TEMPLATE FOR TRANSLATION which works in every instance in which Standard English is translated into C.

Next in the GGF we have "statement" (again without the quotes). This refers to any valid C expression. It could also be called expression in the GGF but A&J are trying to avoid confusion. Otherwise it could be

if (expression-1)
expression-2;

The statement or expression-2 could be the C code for printing out A equals B. Thus, this example in Standard English would be:

If A=B,
then print out A equals B.

In C that translates into:

if (A= = B)
   printf ("A equals B");

C has its little symbols which we homo sapiens call punctuation marks. The Standard English above has a comma and period. The snippet of C code above has NO punctuation mark after the first line but a ; after the second line.
CHOICE BEHAVIOUR

How does the if statement in C enable the machine to make choices or decisions? Because the sequence of steps in program execution is exactly the same as the choice in a basic T or Y maze. In the maze we examine the first choice. Is A equal to B? If it is (if the maze logic says "Yes") then we print out "A equals B". If A is not equal to B (if the maze logic says "No") then we go to the second branch of the maze. In C, program execution passes to the next line of code.

Here is a snippet of code which A&J give on page 70 to illustrate this:

if (x= =y)
    printf ("x is equal to y");
else
    if (x>y)
          printf ("x is greater than y");
     else
          printf ("x is smaller than y");

(The indentations and white space are arbitrary as far as C language is concerned. We can leave as much blank or white space as we want and indent as we wish. These conventions are to make it easier for the reader.)

The use of "else" in C also seems to be an optional convenience. The above could be written as:

if (x= =y)
    printf ("x is equal to y");
if (x>y)
     printf ("x is greater than y");
printf ("x is smaller than y");

The choice or decision behaviour is that IF x is equal to y, THEN the decision is made to print as above. IF x is not equal to y then THEN second branch of the maze is taken and that branch branches again to allow the machine another choice: is x greater than or smaller than y?
<http://www.geocities.com/machine_psychology/Teach_Yourself_C_p3>
Hosted by www.Geocities.ws

1