PERL/C2/More-Conditional-statements/English

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

Title Of Script: if-elsif-else, switch statement

Author: Amol Brahmankar

Keywords: Conditional Statements in Perl, if-elsif-else, switch statement in perl video tutorial.


Visual Cue
Narration
Slide Welcome to the spoken tutorial on if-elsif-else and switch conditional statements in Perl.
Slide: Learning Objectives In this tutorial, we will learn about the
  • if-elsif-else statement and
  • switch 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


and knowledge of


  • for, foreach, while and do-while loops and
  • if and if-else statements

will be an added advantage.


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

Slide If-elsif-else statement in Perl can be used
  • to test multiple conditions and
  • When all conditions fail then it executes the default else block


Slide


if (condition) {

piece of code;

} elsif (condition) {

another piece of code;

} else {

code to be executed if both the above

conditions are false;

}

The syntax of if-elsif-else statement is as follows


if space open bracket condition close bracket space Open curly bracket

Press Enter

piece of code semicolon

to be executed when the condition is true

Press Enter


Close curly bracket space elsif space open bracket condition close bracket space open curly bracket

Press Enter

another piece of code semicolon

to be executed when elsif condition is true

Press Enter

close curly bracket space else space open curly bracket

Enter

code to be executed when both the above conditions are false semicolon

Press Enter

close curly bracket

Slide

Highlight if (condition)


Highlight elsif (condition)


Highlight else

First, the if condition is checked and executed if the condition is true.


If not, then the else if condition is checked and executed if it is true.


Otherwise, the code within else block is executed.

Now let us look at an example of if-elsif-else 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

$language = 'Perl';


if ($language eq 'Perl') {

print “Hi, I am Perl\n”;

} elsif ($ language eq 'Java') {

print “Hi, I am Java\n”;

} else {

print “I am not a computer language\n”;

}

This will open the conditionalBlocks.pl file in gedit.


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



Highlight variables declaration We have assigned the value 'Perl' to variable language.
Highlight eq Note that, eq is the string comparison operator.
Highlight the if-elsif-else code And then we have the various conditons that we want to check here.
Press Ctrl S Now, press ctrl+s to save the file.
Switch to terminal Then switch to the terminal and execute file directly.
Type

perl conditionalBlocks.pl

Don't press Enter.

Type,

perl conditionalBlocks dot pl

Note:
  1. I am skipping the compilation step. It is not a mandatory step for executing Perl scripts.
  2. If there is any compilation error,
  • execution will throw an error and
  • will stop execution of script


Press Enter. Now press Enter.
Highlight the output on the terminal


Hi, I am Perl

The output shown on the terminal is


Hi, I am Perl

Switch to gedit Now, let us look at our next case.

Switch to gedit.

Change $language = 'Java'; Assign 'Java' to variable language as shown.
Press Ctrl S Press ctrl+s to save the file.
Switch to terminal


perl conditionalBlocks.pl

Switch to the terminal and execute the file.

Type

perl conditionalBlocks dot pl


and press Enter.

Highlight the output on the terminal


Hi, I am Java

The output shown on terminal is


Hi, I am Java

Switch to gedit Again let us switch back to gedit.
Change $language = 'English'; Now, let us assign 'English' to the language variable.
Press Ctrl S Again, press ctrl+s to save the file.
Switch to terminal


perl conditionalBlocks.pl

Switch to the terminal and execute the file.


Type

perl conditionalBlocks dot pl


and press Enter.

Highlight the output on the terminal


I am not a computer language

The output shown on terminal is


I am not a computer language

Slide The 3 cases imply that;
  • Only one if block that satisfies the condition will be executed
  • otherwise the default else block will be executed.


Slide We can have multiple elsif conditions as per our requirement, like this.

<<Don't read out the remaining part just display>>

if (condition) { code1; }

elsif (condition) { code2; }

elsif (condition) { code3; }

else { default_code;}

Slide: Assignment Here is an assignment for you -


Write an if-elsif-else statement to print

  • “I am a Science graduate” if stream is science
  • “I am a Commerce graduate” if stream is commerce
  • “I am an Arts graduate” if stream is neither science or commerce.


<<Pause>>

Now let us learn about the switch statement.

Slide Till Perl 5.8, there was no switch statement in Perl.


After that, Switch module was introduced,


which provided the functionality of switch statement.

Remain on previous slide Note:

Modules in Perl will be covered in subsequent tutorials.

use Switch;


switch ($value) {

case 1 { executes when

$value = 1}

case 'a' { executes when

$value = 'a'}

else { executes when $value does

not match any of the

cases}

}

The syntax of switch is as follows:


use Switch semicolon

Press Enter

switch space open bracket dollar value close bracket space open curly bracket

Press Enter


case space 1 space open curly bracket executes when dollar value equal to 1 close curly bracket.

Press Enter


case space single quote a single quote space open curly bracket executes when dollar value equal to single quote a single quote close curly bracket

Press Enter


else space open curly bracket executes when dollar value does not match any of the cases

close curly bracket

Press Enter

close curly bracket

Let us understand switch using a sample program.
Switch to the Terminal and type


gedit sampleSwitch.pl &


and press Enter.

Open the Terminal and type


gedit sampleSwitch dot pl space ampersand


and press Enter

use Switch;

$var = 'Perl'


switch ($var) {

case 'Perl' {print “I am Perl\n”;}

case 'Java' {print “I am Java\n”;}

case 'Linux' {prin “I am Linux\n”;}

else {print “I am not a computer language\n”;}

}

Now, type the sample program as shown on the screen.



Gedit Let us understand how the switch statement works.
Highlight use Switch The use Switch statement includes the Switch module inside the Perl code.
Note:

We will learn about use keyword in detail in subsequent tutorials.

Now we'll test the different cases.
Highlight $var = 'Perl' We have assigned 'Perl' to the variable $var
Highlight switch ($var) The value in variable $var is checked in the switch statement.
Highlight case 'Perl' In the first case, it matches with the case 'Perl'.
Highlight {print “I am Perl\n”;} So the code written against this case will be executed.
Press ctrl+s Press ctrl+s to save the file.
Switch to terminal

perl sampleSwitch.pl

Now, switch to terminal and execute the script


perl sampleSwitch.pl

press Enter

Terminal

Highlight the output on terminal

The following output is shown on the terminal

I am Perl

Switch back to sampleSwitch.pl in gedit.
Change $var = 'Linux' Now, let us assign 'Linux' to the variable $var
Highlight switch ($var) Again, the value of variable $var will be checked in switch.
Highlight case 'Linux' It matches with the case 'Linux'
Highlight {print “I am Linux\n”;} So code written against this case will get executed.
Switch to terminal

perl sampleSwitch.pl

Now, switch to terminal and execute the script


perl sampleSwitch.pl


Press Enter

Terminal

Highlight the output on terminal

The following output is shown on the terminal

I am Linux

Switch to gedit. Switch to sampleSwitch.pl in gedit.
Change $var = ''Java'' >>

Highlight case 'Java'


<Don't execute this part>

Similarly, if variable $var has value 'Java', then second case will be checked.
Change $var = 'English' Now, let us assign 'English' to the variable $var
Highlight switch ($var) Again, the value of variable $var will be checked in switch.
Point to the 3 case statements >> then point to the else statement It does not match any of the case statements.


So the else statement will be executed.

Switch to Highlight

perl sampleSwitch.pl

Now, switch to terminal and execute the script


perl sampleSwitch.pl


Press Enter

Terminal

Highlight the output on Highlight

The following output is shown on the terminal -

I am not a computer language

Slide The 3 cases imply that:
  • The value of the expression decides the case to be executed
  • Only the valid case will be executed and
  • When there is no valid case, then the default else case will be executed


Slide

Highlight else case

It is not mandatory to write the else case.


In such a scenario,

  • if none of the cases match
  • then there will be no output from the switch statement.


Slide: Assignment Here is another assignment for you -
  • Re-write the previous assignment
  • given earlier in this tutorial
  • using switch statement.


Slide: Summary Let us summarize.

In this tutorial, we have learnt -

  • if-elsif-else statement and
  • switch statement in Perl
  • using sample programs.


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