| << 5.0.0- ASP Control Structures | Chapter5 | 5.1.1- Two Kinds of Statements >> |
An Example in Plain English
Before we begin to look at ASP code let's think about these ideas in simple English for a business situation that you can easily picture in your mind. Let's consider the type of instructions a mechanic might be given by their boss. The code you have written so far is similar to the mechanic's boss telling them to:
- Take truck number one to the fuel pump.
- Fill up the tank with gasoline.
- Bring the truck back to its parking spot.
Although these instructions are very clear and easy to follow in order of line one, line two, line three, they do not cover some possible problems. What if the truck is already full of gasoline? What if the truck needs oil? What about providing service for the other trucks?
In order to get the job done the boss needs to provide a more complex set of instructions, for example:
1. Check if truck number one needs gasoline.
2. If truck number one needs gasoline, then take it to the fuel pump and fill it.
3. If the truck does not need gasoline then skip the filling and go to the next step.
4. Check if truck number one needs oil.
5. If truck number one needs oil, then get out the truck's manual and follow the instructions for adding oil. After you are done adding oil come back and finish these instructions.
6. If the truck does not need oil then skip the oil step and go to the next step.
7. When truck one is full of gas and full of oil, bring it back to its parking spot.
8. When you are done with truck one, repeat the above steps for the rest of the trucks.
The second set of orders will work better for three reasons. First, it covers a range of possibilities such as the gasoline and/or oil not needing refilling. Second, it takes advantage of existing instructions – the truck manual's pages for how to add oil. Third, it gives sufficient instructions to get the job done on the whole fleet of trucks.
This simple example illustrates the three types of constructs that you will use as an ASP programmer:
- Branching statements such as lines 2 and 3: If the truck needs gasoline then fill it up, but if it does not need gasoline then skip the filling up step.
- Jumping statements such as line 5: if you need oil, then stop executing these instructions and instead follow the instructions for adding oil. When you have finished following those adding oil instructions, then come back and continue with these instructions.
- Looping statements such as line 8: when you are done with truck one, go back and repeat these same steps for truck two, then truck three and so on until all of the trucks have been serviced.
| << 5.0.0- ASP Control Structures | Chapter5 | 5.1.1- Two Kinds of Statements >> |

RSS

