Once a decision was made, I did not worry about it afterward.
Calculations and expressions are only a small part of computer programming. Decision and control statements are needed. They specify the order in which statements are to be executed.
So far, we have constructed linear programs, that is, programs that execute in a straight line, one statement after another. In this chapter, we will see how to change the control flow of a program with branching statements and looping statements. Branching statements cause one section of code to be executed or not executed, depending on a conditional clause. Looping statements are used to repeat a section of code a number of times or until some condition occurs.
The if statement allows us to put some decision-making into our programs. The general form of the if statement is:
If the condition is true (nonzero), the statement will be executed. If the condition is false (0), the statement will not be executed. For example, suppose we are writing a billing program. At the end, if the customer owes us nothing or has a credit (owes us a negative amount), we want to print a message. In C, this program is written:
if (total_owed <= 0) printf("You owe nothing.\n");
<= is a
relational operator that represents less than or equal
to. This statement reads “if the
total_owed is less than or equal to zero, print the message.” The complete list of relational ...