Suppose that we wanted to write a C program that prompted the user for
and t and reported the block's distance from its original position. Why
would such a program have to make a decision and act accordingly?
Click here for the answer
We have implemented the block sliding problem as ``block1.c'' in your
``examples'' directory, which you should read into Emacs. You can also
view it directly.
If you look towards the end of the main function you'll see an example of
an if statement. It looks like this:
if ( ... ) {
... <-- The "then part."
...
}
else {
... <-- The "else part."
...
}
The condition to be tested goes inside the parentheses on the first line. When
the condition is true, the (one or more) statements within the then
part are executed in order. But when the condition is false, the (one or
more) statements within the else part are executed instead.
Notice that semicolons do not go after the curly braces that end the
if part and the then part. Notice also how we used indentation to make
the structure of the statement more clear. Finally, notice that the word
``then'' doesn't appear anywhere in the statement.
Compile and run the program a few times, making sure that you understand how
the if statement works.
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah