0% found this document useful (0 votes)
20 views

Unit 3

Uploaded by

Priyanka Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit 3

Uploaded by

Priyanka Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 117

Unit-3

Perl
Practical Extraction and Reporting Language
Introduction to Perl

• Perl is a programming language developed by Larry Wall in 1987.

• Perl is a general-purpose, high level interpreted and dynamic programming language.

• There is no official Full form of the Perl, but still, the most used expansion is “Practical
Extraction and Reporting Language“.

• Perl supports both the procedural and Object-Oriented programming.

• Perl is a lot similar to C syntactically and is easy for the users who have knowledge of C
, C++.
Why Perl?

• Easy to start: Perl is a high-level language so it is closer to other popular programming


languages like C, C++ and thus, becomes easy to learn for anyone.
• Text-Processing: As the acronym “Practical Extraction and Reporting Language” suggest
that Perl has the high text manipulation abilities by which it can generate reports from
different text files easily. Also, it can convert the files into some another form.
• Contained best Features: Perl contains the features of different languages like C,
sed(stream editor), awk(Aho, Weinberger, and Kernighan), and sh etc. which makes
the Perl more useful and productive.
A sh( shell script) is a computer program designed to be run by a Unix shell, a
command-line interpreter.
The AWK language is a data-driven scripting language consisting of a set of actions to

be taken against streams of textual data


• System Administration: Due to having the different scripting languages capabilities Perl
make the task of system administration very easy. Instead of becoming dependent on
many languages, just use Perl to complete out the whole task of system administration. In
Spite of this Perl also used in web programming, web automation, GUI programming etc.

• Web and Perl: Perl can be embedded into web servers to increase its processing power
and it has the DBI package, which makes web-database integration very easy.
Applications of Perl

• Perl is one of the most widely used language over the web.

• Perl used to be the most popular web programming language due to its text manipulation
capabilities and rapid development cycle.

• Perl can handle encrypted Web data, including e-commerce transactions.

• Perl's DBI package makes web-database integration easy.

• Perl can be embedded into web servers to speed up processing.


Advantages of Perl:
• Perl Provides supports for cross platform and it is compatible with mark-up languages
like HTML, XML etc.

• It is very efficient in text-manipulation i.e. Regular Expression. It also provides the socket
capability.

• It is free and a Open Source software.

• It is an embeddable language that’s why it can embed in web servers and database servers.

• It supports more than 25, 000 open source modules on which provide many powerful
extensions to the standard library. For example, XML processing, GUI(Graphical User
Interface) and DI(Database Integration) etc.
First Perl Program:
Print “hello world”;
Perl File Extension:
a Perl file must be saved with a .pl or .PL file extension.
Comments in Perl:
# This is a single line comment
=begin comment This is all part of multiline comment. You can use as many lines as
you like These comments will be ignored by the compiler until the next =cut is
encountered. =cut
How to Run a Perl Program?

Generally, there are two ways to Run a Perl program-


• Using Online IDEs: You can use various online IDEs which can be used to run Perl
programs without installing.

• Using Command-Line: You can also use command line options to run a Perl program.
Below steps demonstrate how to run a Perl program on Command line in Windows/Unix
Operating System:
Windows

•First, open a text editor like Notepad or Notepad++.


•Write the code in the text editor and save the file with .pl
extension
•Make sure you have downloaded and installed the latest version
of Perl from https://www.perl.org/get.html
•Open commandline and Run the command perl -v to check if
your Perl’s latest version has been installed properly or not.
• To compile the code type perl HelloWorld.pl. If your code has
no error then it will execute properly and output will be
displayed.
Perl Data Types

• Perl language is a loosely typed language, the Perl interpreter itself chooses the data type.
Hence, there is no need to specify data type in Perl programming.

There are basically three data types in Perl:

• Scalars: Perl scalars are a single data item. They are simple variables, preceded by a ($)
sign. A scalar can be a number, a reference (address of a variable) or a string.
• Arrays: Perl arrays are an ordered list of scalars. They are preceded by (@) sign and
accessed by their index number which starts with 0.
• Hashes: Perl hashes are an unordered collection of key-value pairs. They are preceded by
(%) sign and accessed using keys.
Naming Convention for scalers
Perl has three rules for naming scalars
1.All scalar names will begin with a $. It is easy is to remember to prefix every name with
$.
2.Like PHP. after the first character $, which, is special in Perl, alphanumeric characters i.e.
a to z, A to Z and 0 to 9 are allowed. Underscore character is also allowed. Use
underscore to split the variable names into two words. `But the First character cannot be a
number.
3.Even though numbers can be part of the name, they cannot come immediately after $.
This implies that first character after $ will be either an alphabet or the underscore.
Perl Example:

$var;
$Var32;
$vaRRR43;
$name_underscore_23;
• Two Types of Scalar Data Types
1.Numbers
2.Strings
Numbers:
In this type of scalar data we could specify:

•integers, simply it’s whole numbers, like 2, 0, 534

•floating-point numbers, it’s real numbers, like 3.14, 6.74, 0.333

Notice: In general, Perl interpreter sees integers like floating points numbers. For example,
if you write 2 in your programs, Perl will see it like 2.0000
Floating-point literals:
• It consists of digits, optionally minus, decimal point and exponent.
Strings

• The maximum length of a string in Perl depends upon the amount of memory the
computer has. There is no limit to the size of the string, any amount of characters,
symbols, or words can make up your strings.
• The shortest string has no characters.
• The longest can fill all of the system memory.
Like numbers there are two different types of strings:
• Single quotes string literals
• Double quotes string literals
Single-quoted string literals
• Single quotation marks are used to enclose data
Double-quoted string literals
• Double quotation marks are used to enclose data that needs to be interpolated before
processing. That means that escaped characters and variables aren’t simply literally
inserted into later operations, but are evaluated on the spot. Escape characters can be used
to insert newlines, tabs, etc.
String Concatenation (period) :

• The concatenation operator “.” unites two or more strings


Conversion Between Numbers and Strings:
Multiline Strings
• If you want to introduce multiline strings into your programs, you can use the standard
single quotes.
Perl Array

• An Array is a special type of variable which stores data in the form of a list.
• Each element can be accessed using the index number which will be unique for each and
every element.
• You can store numbers, strings, floating values, etc. in your array.
• In Perl, you can define an array using ‘@’ character followed by the name that you want
to give. Let’s consider defining an array in Perl.
my @array;
3. Hashes(Associative Arrays):

• It is a set of key-value pair.


• It is also termed the Associative Arrays.
• To declare a hash in Perl, we use the ‘%’ sign.
• To access the particular value, we use the ‘$’ symbol which is followed
by the key in braces.
Perl Variables
• Variables are the reserved memory locations to store values. This means that when you
create a variable you reserve some space in memory.
• Based on the data type of a variable, the interpreter allocates memory and decides what
can be stored in the reserved memory. Therefore, by assigning different data types to
variables, you can store integers, decimals, or strings in these variables.
• we are going to use three types of variables in Perl. A scalar variable will precede by a
dollar sign ($) and it can store either a number, a string, or a reference. An array variable
will precede by sign @ and it will store ordered lists of scalars. Finaly, the Hash variable
will precede by sign % and will be used to store sets of key/value pairs.
• Perl maintains every variable type in a separate namespace. So you can, without fear of
conflict, use the same name for a scalar variable, an array, or a hash. This means that $foo
and @foo are two different variables.
Creating Variables
• Perl variables do not have to be explicitly declared to reserve memory space. The
declaration happens automatically when you assign a value to a variable. The equal sign
(=) is used to assign values to variables.

NOTE: Declare a variable before we use it if we use use strict statement in our program.
• The operand to the left of the = operator is the name of the variable, and the operand to
the right of the = operator is the value stored in the variable.
For example −
$age = 25; # An integer assignment
$name = "John "; # A string
$salary = 1445.50; # A floating point
Here 25, "John " and 1445.50 are the values assigned to $age, $name and $salary variables,
respectively.
Scalar Variables

• A scalar is a single unit of data. That data might be an integer number, floating point, a
character, a string, a paragraph, or an entire web page. Simply saying it could be anything,
but only a single thing.

OUTPUT
Array Variables

• An array is a variable that stores an ordered list of scalar values.


• Array variables are preceded by an "at" (@) sign.
• To refer to a single element of an array, you will use the dollar sign ($) with the variable
name followed by the index of the element in square brackets.

OUTPUT
Hash Variables
• A hash is a set of key/value pairs.
• Hash variables are preceded by a percent (%) sign.
• To refer to a single element of a hash, you will use the hash variable name followed by
the "key" associated with the value in curly brackets.
Scope of a variable – Access Modifiers

• We can declare a scalar in anywhere in the program. But you need to specify an access
modifier.
• There are 3 types of modifiers
1.my
2.local
3.our
My:
Using this you can declare any variable which is specific within the block. i.e. within
the curly braces.
My Access Modifiers
MY scope

• Variable declared with my scope at the beginning of the script is


accessible through the script including subroutines.
• Variable declared with my scope inside a subroutine is accessible
within that subroutine.
OUTPUT
OUTPUT
Local
• Local variables accessible within block or a subroutine.
• Using this we can actually mask the same variable values to different
values without actually changing the original value of the variable,
suppose we have a variable $a for which the value is assigned 5, you
can actually change the value of that variable by re-declaring the
same variable using local keyword without altering the original value
of the variable which is 5.
OUTPUT
Our
• Once a variable is declared with access modifier “our” it can be used across the entire
package. Suppose, you have Perl module or a package test.pm which has a variable
declared with scope our. This variable can be accessed in any scripts which will use that
package.
use strict
This will help you write better and cleaner code. ‘use strict’ turns on strict pragma
which will make you declare your variables with my keyword.
OUTPUT
OUTPUT
OUTPUT
Quote-like Operators

• In these operators, {} will represent a pair of delimiters that user choose. In this category
there are three operators as follows:
 q{ } : It will encloses a string in single quotes(”) and cannot interpolate the string
variable. For Example: q{hello} gives ‘hello’.
 qq{ }: It will encloses a string in double quotes(“”) and can interpolate the string
variable. For Example: qq{hello} gives “hello”.
 qx{ } : It will encloses a string in invert quotes(“). For Example: qx{hello} gives `hello`.

• Example:
Operators

• Operators are the main building block of any programming language.


• Operators allow the programmer to perform different kinds of operations on operands.
• In Perl, operators symbols will be different for different kind of operands(like scalars and
string).
• Operators Can be categorized based upon their different functionality:
1. Arithmetic Operators
2.Relational Operators
3.Logical Operators
4.Bitwise Operators
5.Assignment Operators
6.Ternary Operator
Arithmetic Operators: These are used to perform arithmetic/mathematical
operations on operands.
 Addition: ‘+‘ operator is used to add the values of the two operands.

 For Example: $a = 5;

$b = 10;

print $a + $b;

Here Result will be 15

Subtraction: ‘–‘ operator is used to subtract right hand operand from left hand operand.
For Example:

• $a = 10;$b = 5; print $a - $b;

• Here Result will be 5


Multiplication: ‘*‘ operator is used to multiplies the value on either side of the operator.
For Example: $a = 5; $b = 10; print $a * $b;

Here Result will be 50


 Division Operator: ‘/‘ operator returns the remainder when first operand is divided by
the second. For Example: $a = 30; $b = 15; print $a / $b;

Here Result will be 2

Modulus Operator: ‘%‘ operator is used to divide left hand operand from right operand
and returns remainder. For Example: $a = 10; $b = 15; print $a % $b;

Here Result will be 10


 Exponent Operator: ‘**‘ operator is used to perform the exponential(power) calculation on operands.

For Example:

$a = 2;

$b = 3;

print $a**$b;

Here Result will be 8


Relational Operators
• Relational operators are used for comparison of two values.
• These operators will return either 1(true) or nothing(i.e. 0(false)).
• Sometimes these operators are also termed as the Equality Operators.
• These operators have the different symbols to operate on strings.
Equal To Operator: ‘==’ Check if two values are equal or not. If equals then return 1
otherwise return nothing.

Not equal To Operator: ‘!=’ Check if the two values are equal or not. If not equal then
returns 1 otherwise returns nothing.

Comparison of equal to Operator: ‘< = >’ If left operand is less than right then returns -1,
if equal returns 0 else returns 1.

Greater than Operator: ‘>’ If left operand is greater than right returns 1 else returns
nothing.
 Less than Operator: ‘<‘ If left operand is lesser than right returns 1 else returns
nothing.
 Greater than equal to Operator: ‘>=’ If left operand is greater than or equal to right
returns 1 else returns nothing.
 Less than equal to Operator: ‘<=’ If left operand is lesser than or equal to right returns
1 else returns nothing.
Logical Operators
• These operators are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition in consideration.
 Logical AND: The ‘and’ operator returns true when both the conditions in consideration
are satisfied. For example, $a and $b is true when both a and b both are true (i.e. non-
zero). You can use also &&.
 Logical OR: The ‘or’ operator returns true when one (or both) of the conditions in
consideration is satisfied. For example, $a or $b is true if one of a or b is true (i.e. non-
zero). Of course, it gives result “true” when both a and b are true. You can use also ||
 Logical NOT: The ‘not’ operator will give 1 if the condition in consideration is satisfied.
For example, not($d) is true if $d is 0.
Bitwise Operators
• These operators are used to perform the bitwise operation. It will first convert the number into bits and
perform the bit-level operation on the operands.

 & (bitwise AND) Takes two numbers as operands and does AND on every bit of two numbers. The result of
AND is 1 only if both bits are 1. For example

$a = 13; // 1101

$b = 5; // 0101

$c = $b & $a; print $c; Here Output will be 5

Explanation:

$a = 1 1 0 1

$b = 0 1 0 1

$c = 0 1 0 1
 | (bitwise OR) Takes two numbers as operands and does OR on every bit of two
numbers. The result of OR is 1 any of the two bits is 1. For example
$a = 13; // 1101
$b = 5; // 0101
$c = $b | $a;
print $c;
• Here Output will be 13
Explanation:
• $a = 1 1 0 1
• $b = 0 1 0 1
• ---------------
$c = 1 1 0 1
---------------
• ^ (bitwise XOR) Takes two numbers as operands and does XOR on every bit of two numbers. The result of
XOR is 1 if the two bits are different. For example

$a = 13; // 1101

$b = 5; // 0101

$c = $b ^ $a;

print $c; Here Output will be 8

$a = 1 1 0 1

$b = 0 1 0 1

-------------

$c = 1 0 0 0
• ~ (Complement Operator) This is unary operator act as flipping bits. It’s work is to reverse the bits and gives
result using 2’s complement form due to a signed binary number.

 (<<) Binary Left Shift Operator will takes two numbers, left shifts the bits of the first operand, the second
operand decides the number of places to shift. It performs multiplication of the left operand by the number of
times specified by the right operand. For Example:

$a = 60;

$c = $a << 2;

print $c;

Output: 240

• Explanation:

• 60 * 2 = 120 ---(1)

• 120 * 2 = 240 ---(2)


 (>>)Binary Right Shift Operator will take two numbers, right shifts the bits of the first
operand, the second operand decides the number of places to shift. It performs division
of the left operand by the number of times specified by right operand. For Example:

$a = 60;

$c = $a >> 2;

print $c;

Output: 15
• Explanation:

• 60 / 2 = 30 ---(1)

• 30 / 2 = 15 ---(2)
Assignment Operators

• Assignment operators are used to assigning a value to a variable. The left side operand of the assignment
operator is a variable and right side operand of the assignment operator is a value.
Different types of assignment operators are shown below:

 “=”(Simple Assignment) : This is the simplest assignment operator. This operator is used to assign the
value on the right to the variable on the left.
Example :

• $a = 10;

• $b = 20;
 “+=”(Add Assignment) : This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the
current value of the variable on left to the value on the right and then assigns the result to the variable on the
left.
Example : ($a += $b) can be written as ($a = $a + $b)

If initially value stored in a is 5. Then ($a += 6) = 11.

 “-=”(Subtract Assignment) : This operator is combination of ‘-‘ and ‘=’ operators. This operator first
subtracts the current value of the variable on left from the value on the right and then assigns the result to
the variable on the left.
Example : ($a -= $b) can be written as ($a = $a - $b)

If initially value stored in a is 8. Then ($a -= 6) = 2.

 “*=”(Multiply Assignment) : This operator is combination of ‘*’ and ‘=’ operators. This operator first
multiplies the current value of the variable on left to the value on the right and then assigns the result to the
variable on the left.
Example : ($a *= $b) can be written as ($a = $a * $b)

 If initially value stored in a is 5. Then ($a *= 6) = 30.


 “/=”(Division Assignment) : This operator is combination of ‘/’ and ‘=’ operators. This operator first
divides the current value of the variable on left by the value on the right and then assigns the result to the
variable on the left.
Example :($a /= $b) can be written as ($a = $a / $b)

If initially value stored in a is 6. Then ($a /= 2) = 3.

 “%=”(Modulus Assignment) : This operator is combination of ‘%’ and ‘=’ operators. This operator first
modulo the current value of the variable on left by the value on the right and then assigns the result to the
variable on the left.
Example : ($a %= $b) can be written as ($a = $a % $b)

If initially value stored in a is 6. Then ($a %= 2) = 0.


 “**=”(Exponent Assignment) : This operator is combination of ‘**’ and ‘=’ operators. This operator first
exponent the current value of the variable on left by the value on the right and then assigns the result to the
variable on the left.
Example :

($a **= $b) can be written as ($a = $a ** $b)

If initially, value stored in a is 6. Then ($a **= 2) = 36.


Ternary Operator

• It is a conditional operator which is a shorthand version of if-else statement. It has three operands and hence
the name ternary. It will return one of two values depending on the value of a Boolean expression.

Syntax:

• condition ? first_expression : second_expression;

Explanation:

• condition: It must be evaluated to true or false.

• If the condition is true

• first_expression is evaluated and becomes the result.

• If the condition is false,

• second_expression is evaluated and becomes the result.


String Manipulation Operators
• String are scalar variables and start with ($) sign in Perl. The String is defined by the user
within a single quote(”) or double quote(“”). There are different types of string operators
in Perl, as follows:
 Concatenation Operator (.) : Perl strings are concatenated with a Dot(.) symbol. The
Dot(.) sign is used instead of (+) sign in Perl.
 Repetition Operator (x): The x operator accepts a string on its left-hand side and a
number on its right-hand side. It will return the string on the left-hand side repeated as
many times as the value on the right-hand side.
Range Operator(..)

• In Perl, range operator is used for creating the specified sequence range of specified
elements.
• This operator is used to create a specified sequence range in which both the starting and
ending element will be inclusive.
For example, 7 .. 10 will create a sequence like 7, 8, 9, 10.
Auto Increment & Auto Decrement Operator
• Auto Increment(++): Increment the value of an integer.
• When placed before the variable name (also called pre-increment operator), its value is incremented instantly.
For example, ++$x.
• when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily
until the execution of this statement and it gets updated before the execution of the next statement.
For example, $x++.
Auto Decrement Operator(--): Decrement the value of an integer.
• When placed before the variable name (also called pre-decrement operator), its value is decremented instantly.
For example, –$x.
• when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily
until the execution of this statement and it gets updated before the execution of the next statement.
For example, $x–.
Note: The increment and decrement operators are called unary operators as they work with a single operand.
Arrow Operator(->)

• This operator is used for dereferencing a Variable or a Method from a class or an object.
Example: $ob->$x
is an example to access variable $x from object $ob.
Mostly this operator is used as a reference to a hash or an array to access the elements of the
hash or the array.
• The right-hand side of the operator may be an array subscript ([...]), a hash subscript
({...}), or a subroutine argument ((...)). In such cases, the left-hand side must reference an
array, a hash, or a subroutine, respectively.
• For example:
• $ arr->[7] # an array dereference

$ hash->{"something"} # a hash dereference

$ subroutine->(1,2) # a subroutine dereference

• The left-hand side of the operator may also reference an object or a class name.
Conditional statements

• Conditional statements are used to decide the flow of execution based on different conditions.
1. if Statement
2. if -else statement
3. if-elsladder
4. unless
5. unless-else
6. unless-elsif
1.Perl If statement
It evaluates the content only if expression is true.
Syntax:
if(expression)
{
//content to be evaluated
}

Note : If the curly brackets { } are not used with if statements then there will
be compile time error. So it is must to use the brackets { } with if statement.
Perl If-else Example

• The Perl if-else statement is used to execute a code if


condition is true or false. The syntax of if-else statement
is given below:
if(expression)
{
//code to be executed if condition is true
}else
{
//code to be executed if condition is false
}
Perl If-else Example with Input from user

In this example, we'll take input from user by using standard input (<STDIN>/<>).
Perl If else-if Example
• The Perl if else-if statement executes one code from multiple conditions. The syntax of if
else-if statement is given below:
• As soon as one of the conditions controlling the if is true, the statement associated with
that get executed, and the rest of the ladder is bypassed. If none of the conditions is true,
then the final else statement will be executed.
if(condition1)
{
//code to be executed if condition 1is true
}
elsif(condition2){
//code to be executed if condition2 is true }
elsif(condition3){
//code to be executed if condition3 is true }
else{
//code to be executed if all the conditions are false }
Nested – if Statement

• if statement inside an if statement is known as nested if. if statement in this case is the
target of another if or else statement. When more than one condition needs to be true and
one of the condition is the sub-condition of parent condition, nested if can be used.
Syntax :
if (condition1)
{
# Executes when condition1 is true
if (condition2)
{
# Executes when condition2 is true
}
}
# Perl program to illustrate
# Nested if statement

$a = 10;

if($a % 2 ==0)
{
if($a % 5 == 0)
{
printf "Number is divisible by 2 and 5\n";
}
unless Statement
• In this case if the condition is false then the statements
will execute. The number 0, the empty string “”,
character ‘0’, the empty list (), and undef are
all false in a boolean context and all other values are
true.
Syntax :
• Unless(boolean_expression)
{
# will execute if the given condition is false
}
Unless statement program
Unless-else Statement
• Unless statement can be followed by an optional else statement, which executes when the
boolean expression is true.
Syntax :
unless(boolean_expression)
{
# execute if the given condition is false
}
else {
# execute if the given condition is true
}
Unless – elsif Statement
• Unless statement can be followed by an optional elsif…else statement, which is very
useful to test the various conditions using single unless…elsif statement.
Perl given-when Statement

given-when statement in Perl is a substitute for long if-statements that compare a


variable to several integral values.
•The given-when statement is a multiway branch statement.
•It provides an easy way to dispatch execution to different parts of code
based on the value of the expression.
•given is a control statement that allows a value to change control of execution.
given-when in Perl is similar to the switch-case of C/C++, Python or PHP.
Just like the switch statement, it also substitutes multiple if-statements with different cases.
given-when statements also use two other keywords along with it, namely break and continue.
These keywords maintain the flow of the program and help in getting out of the program execution or
skipping execution at a particular value.
break: break keyword is used to break out of a when block.
In Perl, there is no need to explicitly write the break after every when block. It is already defined
implicitly.
continue: continue, on the other hand, moves to the next when block if first when block is
correct.
Loops in perl
• There may be a situation when you need to execute a block of code several number of
times.
• Perl programming language provides the following types of loop to handle the looping
requirements.
1.while
2.do-while
3.for
4.foreach
5.until
while Loop

• A while loop generally takes an expression in parenthesis.


• If the expression is True then the code within the body of while loop is executed.
• A while loop is used when we don’t know the number of times we want the loop to be
executed however we know the termination condition of the loop.
• It is also known as a entry controlled loop as the condition is checked before executing
the loop. The while loop can be thought of as a repeating if statement.
Infinite While Loop:

• While loop can execute infinite times which means there is no terminating condition for
this loop.
• In other words, we can say there are some conditions which always remain true, which
causes while loop to execute infinite times or we can say it never terminates.
do…. while loop
• A do..while loop is almost same as a while loop.
• The only difference is that do..while loop runs at least one time.
• The condition is checked after the first execution.
• A do..while loop is used when we want the loop to run at least one time.
• It is also known as exit controlled loop as the condition is checked after executing the
loop.
until loop

• until loop is the opposite of while loop. It takes a condition in the parenthesis and it only
runs until the condition is false. Basically, it repeats an instruction or set of instruction
until the condition is FALSE. It is also entry controller loop i.e. first the condition is
checked then set of instructions inside a block is executed.
for Loop

• “for” loop provides a concise way of writing the loop structure. Unlike a while loop, a
for statement consumes the initialization, condition and increment/decrement in one line
thereby providing a shorter, easy to debug structure of looping.
• Syntax:
foreach Loop

• A foreach loop is used to iterate over a list and the variable holds the value of the
elements of the list one at a time.
• It is majorly used when we have a set of data in a list and we want to iterate over the
elements of the list instead of iterating over its range. The process of iteration of each
element is done automatically by the loop.
Nested Loops

• A nested loop is a loop inside a loop. Nested loops are


also supported by Perl Programming.
• Nested for loop
Nested foreach loop
Nested while loop
Nested do..while loop
Nested until loop

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy