PERL/C2/Conditional-statements/English
Title Of Script: if, if-else statements in Perl
Author: Amol Brahmankar
Keywords: Conditional Statements in Perl, if statement in perl, if-else statement in perl, video tutorial.
| |
|
| Slide | Welcome to the spoken tutorial on if and if-else conditional statements in Perl. |
| Slide: Learning Objectives | In this tutorial, we will learn about;
if statement and if-else statement in Perl |
| Slide: System Requirements | I am using
|
| Slide: Prerequisites | You should have basic knowledge of Variables and Comments in Perl
|
| Slide | Perl provides the following conditional statements -
1. if 2. if-else 3. if-elsif-else and 4. switch
|
| Slide | if statement in Perl can be used
|
| Slide
piece of code; } |
The syntax of if conditional statement is as follows
Enter Piece of code to be executed when the condition is true semicolon Enter Close curly bracket |
| Highlight if (condition) | The code inside the if statement will be executed only when the condition is true. |
| Now let us look at an example of if statement. | |
| Switch to the Terminal and type
|
Open the Terminal and type
|
| Point to the filename conditionalBlocks.pl in the Titlebar of gedit.
$count = 5;
print “I am inside if statement\n”; }
|
This will open the conditionalBlocks.pl file in gedit.
|
| Highlight == | Note the equal to equal to sign here. This is the comparison operator. |
| Highlight if ($count == 5) | The condition $count equal to equal to 5 is checked against the value of variable count.
|
| Press Ctrl S | Now, press ctrl+s to save the file. |
| Switch to terminal | Then switch to the terminal.
|
| perl -c conditionalBlocks.pl
|
Type the following to check for any compilation or syntax error -
|
| Highlight the below line on terminal-
|
The following line will be displayed on the terminal window
conditionalBlocks.pl syntax OK |
| Execute Perl script
|
As there is no compilation or syntax error, we will execute the Perl script by typing -
|
| Highlight the output on the terminal
|
The following output will be shown on terminal.
|
| Switch back to gedit
|
Switch back to gedit
|
| Now, let us look at if-else statement. | |
| Slide | This statement is used when user wants to execute
|
| Slide
if-else statement syntax is
piece of code; # executes when if condition is true } else { another piece of code; # executes when if condition is false } |
The syntax for if-else condition is as follows -
to be executed when if condition is true, Press Enter
to be executed when if condition is false Press Enter close curly bracket |
| Switch to gedit
$count = 4; if ($count == 5) { print “I am inside if statement\n”; } else { print “I am inside else statement\n”; } |
Now again, go to the conditionalBlocks.pl file which we have already created in gedit.
else space open curly bracket press Enter
|
| Highlight $count = 4; | Here, 4 is assigned to variable count. |
| Highlight if ($count == 5) | As the value of count variable does not match 5,
|
| Press Ctrl+s | Now press Ctrl+S to save the file. |
| Switch to terminal | Now switch to terminal. |
| perl -c conditionalBlocks.pl
|
Then compile the file for any syntax or compilation errors by typing
|
| Point to the below line on terminal -
|
The following line will be displayed on the terminal
|
| Execute Perl script
|
As there is no compilation or syntax error, we will now execute the Perl script.
perl conditionalBlocks dot pl
|
| Highlight output on the terminal
|
The following output will be shown on terminal.
|
| Slide: Summary | Let us summarize.
In this tutorial, we have learnt - if and if-else conditional statements in Perl using sample programs. |
| Slide: Assignment | Here is assignment for you -
|
| About the Project | Watch the video available at the following link
download and watch it |
| Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org |
| Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
spoken hypen tutorial dot org slash NMEICT hyphen Intro |
| Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|