Perl - Part 10
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
if Construct Continued: Task 007
The last program made used an if...else to determine which possible output to display. But the if..else construct can be as simple or as complex as you need. In the example below, in the case of a voter they're also informed if they are entitled to free-travel.
Modify the responder.pl file to look as below:

When you run the program now, you should get output for all three possible age conditions, as well as whether an over 18 is also over 65 and therefore entitled to free travel:
- Less than 18.
- Age equal to 18.
- Age greater than 18.
- Possibly entitled to free travel.
Nesting
In the above program, we have used an if..else construction. Within the actions to be taken in the event of the first if yielding a TRUE result is another if construct. Placing one construct inside another of it's own type is called nesting. At first it may seem strange but is very useful for making successive decisions, one after the other.
General form of the if statement
The general form of the if statement without specific vaiables, just the structure, is:
if (condition)
{
Set of actions if TRUE;
}
else
{
Set of actions if FALSE;
}
Memorise the general form; apart from it being a common exam question, have it clear how many brackets of each type are present, etc, to avoid mistakes when programming.
Task 008
Modify the responder.pl program to state that the user is or is not entitled to free travel, as appropriate.
Task 009
Modify the responder.pl program to wish the user, when un-entitled to vote, a happy childhood, or happy teenage years as appropriate. HINT(again): Don't re-invent the wheel
More anon (Sam plays it again)...
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
Last updated: 20131015-16:45