Book Cover

Introduction to Scientific Programming
Computational Problem Solving Using:
Maple and C
Mathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

Conditionals Tutorial

In this tutorial you will experiment with relational operators, Boolean operators, and conditional statements in C, as discussed in Chapter 12. You will be using some example programs in this laboratory. You can use your Web browser to view or download them as you prefer.

Simple Conditionals

Take a look at if-1.c. Try to predict exactly what will be printed out when the program runs. Then compile and run it and see if you were right.

Here is an explanation, keyed to the example numbers from the comments.

  1. This is a straightforward if statement. Since the condition is true, the two statements in the body are executed.

  2. In this case the condition is false, so the body is not executed.

  3. This example contains a mistake: a semicolon (;) has inadvertently been inserted following the condition. Unfortunately, this is a legal C program. C interprets the semicolon as marking the end of the if, so it treats the two statements enclosed in braces as the next statement to execute. Since they are not part of the if, the two statements are executed even though the condition was false.

  4. If you don't enclose the body of an if in braces, C will treat the very next statement as the body. Thus, the second printf always executes regardless of the value of the condition. Reindent the Emacs buffer and you will see that the indentation gives you a valuable clue as to what's going on.

Multi-way Conditionals

Take a look at if-2.c. Try to predict exactly what will be printed out when the program runs. Then compile and run it to see if you were right.

Here is an explanation, keyed to the example numbers from the comments.

  1. This example shows an if/else. Because the condition is true, the if part is executed.

  2. Here the condition is false, so the else part is executed.

  3. Here there are three choices. The first condition is false, but the second is true, so the statement that goes with the ``else if'' part is executed.

  4. This is another three-way conditional. This time both of the conditions are true. When this happens, the first true condition takes precedence.

  5. This example is structured just like example 4, except we have omitted the ``else'' from the ``else if''. As a result, C treats this as an ``if'' statement followed by a separate ``if else'' statement.
  6. The body of an ``if'' can itself be an ``if'' statement. This example is

    hard to understand until you use Emacs to indent it, when the nesting structure should make things clear. Remember: the indenting doesn't mean anything to the C compiler, but it can mean a lot to a human reader.

Relational Operators

Take a look at if-3.c. Try to predict exactly what will be printed out when the program runs. Then compile and run it to see if you were right.

The only example that requires an explanation is example 7. It is a common mistake in C to use the assignment operator (=) where the equality operator (==) was intended. Unfortunately, the result is often legal C syntax so the compiler doesn't complain. Even though it is always the case that z == z, the assignment z = z results (in this case) in the else branch being take.

Boolean Operators

Take a look at if-4.c. Try to predict exactly what will be printed out when the program runs. Then compile and run it to see if you were right.

These examples show how the Boolean operators && (and), || (or), and ! (not) can be used to compose more complicated conditions.