PERL/C2/Conditional-statements/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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.


Visual Cue
Narration
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


Ubuntu Linux12.04 operating system and Perl 5.14.2


I will also be using the gedit Text Editor.


You can use any text editor of your choice.

Slide: Prerequisites You should have basic knowledge of Variables and Comments in Perl


Knowledge of for, foreach, while and do-while loops in Perl will be an added advantage.


Please go through the relevant spoken tutorials on the spoken tutorial website.

Slide Perl provides the following conditional statements -

1. if

2. if-else

3. if-elsif-else and

4. switch


In this tutorial, we'll cover if and If-else statements

Slide if statement in Perl can be used
  • to execute a piece of code
  • only when a specified condition is satisfied.


Slide


if (condition) {

piece of code;

}

The syntax of if conditional statement is as follows


if space open bracket condition close bracket space Open curly bracket

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


gedit conditionalBlocks.pl &


and press Enter.

Open the Terminal and type


gedit conditionalBlocks dot pl space ampersand


and press Enter

Point to the filename conditionalBlocks.pl in the Titlebar of gedit.


Type the piece of code.


#!/usr/bin/perl

$count = 5;


if ($count == 5) {

print “I am inside if statement\n”;

}


This will open the conditionalBlocks.pl file in gedit.


Type the following piece of code as displayed on the screen.


Here we have specified a condition for if which checks the value of variable count.



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.


When it is equal to 5, the code within the if block will get executed.

Press Ctrl S Now, press ctrl+s to save the file.
Switch to terminal Then switch to the terminal.


Make sure that you are in the directory in which you have saved your file.

perl -c conditionalBlocks.pl


Type the following to check for any compilation or syntax error -


perl hyphen c conditionalBlocks dot pl


and press Enter.

Highlight the below line on terminal-


conditionalBlocks.pl syntax OK

The following line will be displayed on the terminal window

conditionalBlocks.pl syntax OK

Execute Perl script


perl conditionalBlocks.pl

As there is no compilation or syntax error, we will execute the Perl script by typing -


perl conditionalBlocks dot pl


and press Enter

Highlight the output on the terminal


I am inside if statement

The following output will be shown on terminal.


I am inside if statement

Switch back to gedit


print “I am inside if statement\n” if ($count == 5);

Switch back to gedit


Alternately, we can write the above if statement as-


print space double quote I am inside if statement slash n double quote complete space if open bracket dollar count equal to equal to 5 close bracket semicolon.

Now, let us look at if-else statement.
Slide This statement is used when user wants to execute
  • one piece of code when the condition is true and
  • another piece of code when the condition is false


Slide

if-else statement syntax is


if (condition) {

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 -


if space open bracket condition close bracket space open curly bracket Press Enter


piece of code semicolon

to be executed when if condition is true,

Press Enter


close curly bracket space else space open curly bracket Enter


another piece of code semicolon

to be executed when if condition is false

Press Enter close curly bracket

Switch to gedit


#!/usr/bin/perl

$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.


at the end of the if block type space

else

space open curly bracket press Enter


print space double quote I am inside else statement slash n close double quote semicolon


Press Enter and close the curly bracket.

Highlight $count = 4; Here, 4 is assigned to variable count.
Highlight if ($count == 5) As the value of count variable does not match 5,
  • the code within the if block will not get evaluated
  • instead the code within the else block will get evaluated.


Press Ctrl+s Now press Ctrl+S to save the file.
Switch to terminal Now switch to terminal.
perl -c conditionalBlocks.pl


and press Enter

Then compile the file for any syntax or compilation errors by typing


perl hyphen c conditionalBlocks dot pl


and press Enter

Point to the below line on terminal -


conditionalBlocks.pl syntax OK

The following line will be displayed on the terminal


conditionalBlocks.pl syntax OK

Execute Perl script


perl conditionalBlocks.pl


and press Enter

As there is no compilation or syntax error, we will now execute the Perl script.


Type

perl conditionalBlocks dot pl


and press Enter

Highlight output on the terminal


I am inside else statement

The following output will be shown on terminal.


I am inside else statement

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 -


Print “It is an open source language”

  • when the variable declared has value 'Perl'
  • otherwise print “It's a proprietary language”


About the Project Watch the video available at the following link


It summaries the Spoken Tutorial project


If you do not have good bandwidth, you can

download and watch it

Spoken Tutorial Workshops The Spoken Tutorial Project Team


Conducts workshops using spoken tutorials


Gives certificates to those who pass an online

test


For more details, please write to

contact at spoken hyphen tutorial dot org

Acknowledgment


http://spoken-tutorial.org\NMEICT-Intro

Spoken Tutorial Project is a part of the Talk to a

Teacher project


It is supported by the National Mission on

Education through ICT, MHRD, Government of India.


More information on this Mission is available

spoken hypen tutorial dot org slash NMEICT hyphen Intro

Hope you enjoyed this Perl tutorial.

This is Amol Brahmankar signing off.


Thanks for joining

Contributors and Content Editors

AmolBrahmankar, PoojaMoolya