- IF / ELSE
- IF / ELSIF / ELSE
The IF and ELSE Control Structure
In our past tutorial on the IF conditional statement, we learned that this statement allows a certain piece of code (within the IF) to be executed only by a test, where an expression must result in the TRUE value.However, the code only executes if the test is true.
What if it's false? This is where the ELSE command comes in.
The scope of this structure is:
if ( expressiona ){ # code in case the expression # be true }else{ # code in case the expression # be false }
IF and ELSE Example in Perl
Let's redo the exercise:- You have been hired to create a script for a nightclub. In it, you ask the age of the person and if he is 21 or older, warns that he can enter.
Let's add an else command, which fires when the person is under 18, here's how it looks:
#!/usr/bin/perl print "Type your age: "; chomp($age=<STDIN>); if($age>=21){ print "You can come in\n"; }else{ print "You can't come in\n"; }Before, a message would only appear to the user if he was in adulthood, because he entered the IF.
Now if he is a minor, he will fall into ELSE and we will display another message.
Note that we now have greater 'control' over the execution flow of our Perl scripts.
Let's redo the other exercise:
- You have to create a snippet of code where you will be prompted for a password for the user, and will allow them to enter the system only if they enter the correct password, which is rush2112.
#!/usr/bin/perl print "Type your password: "; chomp($password=<STDIN>); if($password eq 'rush2112'){ print "Password correct, entering in the system...\n"; }else{ print "Word password! Intruder detected!\n"; }If you wrog the password, the script will catch you!
Nested IF and ELSE
A very common technique in programming is to use conditional statements within other conditional statements. Let's create a script that takes two numbers and says which is the largest and the smallest, or if they are equal.First, we test if $a is greater $b, if it is, ok, it falls in the first IF and ends.
If it is not, it will fall into the ELSE.
In this ELSE, we already know that $a is not greater than $b, so it is either equal or $b is greater than $a.
Now, inside ELSE, let's use another structure, an IF asking if $b is greater than $a.
If so, warns you and terminates the program.
If $b is not greater than $a (and $a is no longer than $b), then they are equal numbers, and that possibility falls into an ELSE internal to the first ELSE.
Here's how the code looks:
#!/usr/bin/perl print "First number : "; chomp($a=<STDIN>); print "Second number : "; chomp($b=<STDIN>); if($a > $b){ print "$a is greater than $b\n"; }else{ if($b > $a){ print "$b is greater than $a\n"; }else{ print "$a is equal to $b\n"; } }IF can exist on its own, as we saw in the last tutorial.
But for each ELSE, there is its IF.
We put the code well indented, that is, spaced so that we see the pairs of IF and ELSE.
IF / ELSIF / ELSE
Create a program that asks the user for a score from 0 to 10.If it is 9 or older, say that he has scored A.
If you are 8 to 9, say you got a grade B.
If it's 7 to 6, say you got a grade C.
If it's 6 to 7, say you got a grade D.
Below 6 marks F.
First, we test whether the grade is greater than or equal to nine. If it is, we give A.
If it's not greater than or equal to 9, it's because it's less than 9, so let's test if it's greater than 8, if it's got a grade B.
If it is not, it is below 8.
We now test if it is greater than 7, if it is grade C.
If it is not greater than 7, it is smaller. So we test if it is greater than 6, if it is we say it got a grade D.
Finally, if it does not fall in any of the above cases, it falls in the final ELSE and we say it scored F because it scored less than 6.
Here's how the code looks:
#!/usr/bin/perl print "Your grade: "; chomp($grade=<STDIN>); if($grade>=9){ print "Score A.\n" }else{ if($grade>=8){ print "Score B\n"; }else{ if($grade>=7){ print "Score C\n"; }else{ if($grade>=6){ print "Score D\n"; }else{ print "Score F\n"; } } } }Notice how it goes 'breaking' and going to the right.
In a larger and more complex script, this gets horribly ugly.
Note that the excerpt: else {if ... happens more than once.
We can abbreviate these lines to simply: elsif (expression)
Let's see how our code looks like using ELSIF:
#!/usr/bin/perl print "Your grade: "; chomp($grade=<STDIN>); if($grade>=9){ print "Score A.\n" }elsif($grade>=8){ print "Score B\n"; }elsif($grade>=7){ print "Score C\n"; }elsif($grade>=6){ print "Score D\n"; }else{ print "Score F\n"; }Much nicer to see, understand and organized, don't you agree?
No comments:
Post a Comment