The Selection Control Structure (IF-THEN-ELSE)In this lesson, you will
Structured Programming
|
|
The selection control structure is used to make logical decisions. For example, if
the number of hours worked includes overtime hours, you should be paid one and
half of the regular hourly rate for the overtime hours.
The selection control structure is implemented by Perl in the following format:
In the case that the conditional expression is false and nothing needs to be done, then the if statement can be simplified to become: if ( a conditional expression )
In an if statement, the conditional expression must be enclosed within a pair of brackets ( ). The statements to execute must be enclosed within a pair of braces { }. Even if you code an if statement which does nothing, you are required to include a pair of braces { }. For example: if ( $number1 > $number2 )
|
A typical conditional expression uses the following operators
|
A Grade Program | ||
Program 3.2 A Grade Program
This program accepts a grade and displays whether it is pass or fail.
If the conditional expression ($grade >= 50) is true, then the message " You passed the test. " is printed else the message " You failed the test. " is printed. | ||
Filling an Order Program | ||
Program 3.3 Filling an order Program
This program accepts an order and decides whether there are enough items in stock to fill the order.
| ||
|
A Payroll Program | ||||
Program 3.4 A Payroll Program
The above program tests to see if the employee worked overtime. If yes, the entered overtime hours are used to calculate the overtime pay at the one and half the regular rate. The overtime pay is also added to the regular pay. Notice again the statement: is again a two-stage statement in evaluating the variable $regularPay. The $regularPay at the right is to contain the initial value 300. The result of the addition: is assigned back to the $regularPay. | ||||
A Guessing Game Program | ||
|
Program 3.5. A Guessing Game Program An arbitrary number is set for the user to guess.The above program demonstrates the equal test condition using the equal comparison operator (==). It is commonly mixed up with the assignment operator (=). The The What if the user enters 9 which is out of the range between 1 and 8? It is a good practice to weed out the invalid responses and allow only the responses within the valid range to go through the process. The following program illustrates how the range check is done. | ||
An Improved Guessing Game Program | ||
|
Program 3.6. An Improved Guessing Game Program This program validates the input value in the right range before going through the process.
| ||
The Nested IF Structure | |
| Let's go back to Program 3-1, Testing the positive number. A number can be positive, negative, or zero. The if structure we learned so far can only test true or false, therefore, it is called two-way branching. How can we achieve a three-way branching? We need nested if structures. The following program places an if statement in the false block of another if statement to achieve a three-way branching. | |
|
Program 3.7 Testing a number to be positve, zero, or negative The example illustrates that a three-way test requires a nested if statement.In a multi-way branching case, it is important to match the braces for each block present. Missing one of the braces can easily turn into a debugging nightmare. |
Lesson 3 Exercises | |
| |
| M. Mark, 2002.03.18 | | Introduction | Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 | Appendix | |