Chapter 18. Making Choices
All the code used in the preceding lessons has been completely linear. The program follows a series of steps in order with no deviation.
For example, a sales program could multiply a unit price by quantity desired, add several items' values, multiply to get sales tax and shipping costs, and calculate a grand total.
So far there's been no way to perform different steps under different circumstances. For example, the sales program couldn't charge different prices for different quantities purchased or waive shipping charges for orders over $100. It couldn't even check quantities to see if they make sense. In fact, a clever customer could order −1,000 items to get a huge credit!
In this lesson you learn how a program can make decisions. You learn how the program can take different actions based on user inputs and other circumstances.
Programs often need to decide between two or more courses of action. For example:
If it's before 4:00 p.m., ship today. Otherwise, ship tomorrow.
If the user enters an order quantity less than zero, make the user fix it.
If a word processor has unsaved changes, refuse to exit.
Calculate shipping based on order total: $5 if total < $20, $7.50 if total < $50, $10 if total < $75, and free if total ≥ $75.
The basic idea is the same in all of these cases. The program examines a value and takes one of several different actions depending on the value.
The following sections describe the different statements that Visual Basic ...