04 Part 4 Control structures - If, else, while

LinuxChix Perl Course Part 4: Control structures - If, else, while.

Contents

1) If, else
2) While
3) Exercises
4) Where to find out more...

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

1) If, else

Conditional statements in Perl tell your program which way to proceed
through your code.

One of the most useful (along with the for loop) is the if statement.

The basic setup for an if statement is

if (conditional statement 1) { #Note the curly bracket here

<Action code>
}

Example:

if ($money <0){
print "You are overdrawn!\n";
}


The code can be left in this manner, if the condition is not met then
the program will simply ignore that particular block of code.


An alternative to this could be.

if (conditional statement 1) {

<Action code 1>

} if (conditional statement 2){

<Action code 2>

} if (conditional statement 3){

<Action code 3>

}


An alternative block can be added to the code as follows

if (conditional statement 1){

<Action code 1>
} else {

<Action code 2>

}

if ($money < 0) {
print "You are overdrawn!"\n;
} else {
print "You are in credit."\n;
}


# There is an elsif command which may also be used in this situation,
we will meet that later.

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

2) While

A while loop will repeat a block of code when a condition is true.

while (conditional statement){

Block of code

}

Example #$money must be greater than 0 to begin with for this example to work.

while ($money > 0){
print"You are in credit.\n";
$money--; # -- is the autodecrement
}

#If we are testing for something negative, than the unless condition
may be more helpful, we will meet this later.


The importance of the use brackets to denote blocks of code cannot be
over emphasised here.

In C this is acceptable:


if condition do_this();


in Perl, you must do:

if condition {do_this();}


The only change to this would be where the block is a single line, where you
could have:

do_this() if $condition;

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

3) Exercise

Write a program that will ask the user to guess a number. The number
will be predefined at the beginning of your program.

Example

#!usr/bin/perl -w
use strict;

my $target = 17;

#Rest of your program

If you get into trouble please feel free to mail me for hints.
---------------------------

Answers to previous exercises

Part 2

a) The purpose of \n is to force a newline. (The game is given away by
the program title)

b) Here is a version of average.pl

#!/usr/bin/perl -w
use strict;

my $sum = 23+28+17+31+1;

my $average = $sum/5;

print "The average of the five numbers is: $average\n";

# Note: This is not the most efficient program that could be written,
however I have tried to show the steps involved.

c) This is a version of repetition.pl

#!/usr/bin/perl -w
use strict;

print "Suzy\n" x 6;

All futher answers will be published via the website by the first
weekend in May.

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

4) Where to find out more...

An archive of lessons is being created at

http://www.tyneside.pm.org/courseindex.html

Here there are links to useful websites and other books.

Part 5 of the course will follow on 10th May 2003
-----------------------------------------

Note: The author uses Perl version 5.6.1. All exercises should therefore
work on later versions. If you wish to find out which version of Perl you are
running type "perl -v" into your command line.

If you have any comments regarding this Perl lesson, suggestions for future
topics, useful website addresses, useful reference material or you just want
to say what you thought, please send your email to

alice at schiffwood dot co dot uk

I will be pleased to hear from you.

Credits and acknowledgements

Sources of material include:
Learning Perl, Randal Schwartx & Tom Phoenix,
Perl Training Australia (http://www.perltraining.com.au)
Perl 5 Developer's guide, Peschko & DeWolfe
http://learn.perl.org
With very great thanks to Jacinta Richardson and Meryll Larkin for the
course help, and Trish Fraser for the webpages and consumer testing.
--
Alice Wood
Project Gutenberg newsletter editor