For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
Perl module P206
More Loops and Conditionals
Exercises, examples and other material relating to training module P206. This topic is presented on public courses Perl Programming
, Perl bootcamp
, Learning to program in Perl
, Perl Programming
"There's more than one way of doing it," says the cover of Programming Perl, the book co-written by Larry Wall, who wrote the language. He's right Ð there are usually many ways of doing the same thing. That's confusing for the newcomer, but great for the experienced practitioner. This module covers extra syntaxes for "if" and "while", and other loops and conditionals that use different keywords, or no keywords at all.
|Articles and tips on this subject||updated|
|3200||How a for loop works Java, Perl and other languages|
Java, Perl, PHP, Ruby, C, C++, Tcl and many other languages support a "for" loop construct.
When your program enters the top of the loop, it performs the first statement in the brackets ... in this Perl example, that's to initialise the $now variable to 1. It then evaluates the expression given as ...
|3914||While, for, foreach or something else to loop.|
Newcomers to programming often ask "which loop should I use?"
Every language has a while loop, most have a for loop (though sometimes those differ in what they do - Python is an exceptional one, for example), and many have a foreach loop. Then you get the odd ones such as the until loop in Perl ...
|3619||Ruby v Perl - a comparison example|
One of the popular exercises I set on our Perl courses goes something like ...
Write a program to ask the user to enter four numbers each between the value of 1 and 6. If the user enters a number below 1 or over 6, ask him to enter that number again. If the user enters the word END, stop reading numbers.
|3398||Perl - making best use of the flexibility, but also using good coding standards|
Sometimes, examples written during a course are throw-aways ... but at other times they become a useful addition to our resources. On a private private Perl course that I was running yesterday, and continues today and Monday, I found myself writing a useful piece that shows examples of lots of ways of ...
|962||Breaking a loop - Ruby and other languages|
When you're in a loop there are occasions you want to say get me our of this loop NOW, or "I'm done with the current iteration. And those are the 'classic' break and continue statements from C, C++ and Java. Languages like Perl changed break to last and continue to next ... and added a redo that asks ...
|2972||Some more advanced Perl examples from a recent course|
I ran an extra Perl for larger projects course, single company, at the tail end of last week and into the weekend (the only gap in my diary for a few weeks!) and - as is often the case on single-company courses - I wrote some new illustrative code to show specific subjects that came up in a different ...
|2967||Multiway branches in Perl - the given and when syntax|
If you want to perform a multiple alternative branch in Perl, the traditional way has been to use if, a series of elsif (note the spelling!) checks, and a final else if you want to add a default or otherwise condition [example]. Until recent versions, there was no switch statement or equivalent. That's ...
|2892||Alternative loops and conditionals in Ruby and Perl|
Ruby gets a lot from Perl - including its eclecticness in having a wide variety of alternative ways of doing similar things - and no more so than in loops and conditionals. Not only do you have an if but also an unless. Not only do you have a while but also an until. And not only can you write these ...
|2832||Are you learning Perl? Some more examples for you!|
One of the things that you'll get on a Well House Consultants course is new code written by the tutor in front of your very eyes. We do that because:
• you need to learn not only how code works, but also the though code process behind the code - not only how it is written in a ceratyin way but ...
|2824||A pint of Black Rat, and a lazy barman|
The first young lady asked for a pint of Black Rat (the local Melksham cider), and the barman asked her for proof of age. But the second young lady, who asked the for a pint of coke, was served without question.
Typical lazy barman? No - typical efficient barman!
With joined up thinking, so much ...
|2815||switch and case, or given and when in Perl|
There's a lot of well established Perl code running in many places, using versions of the language that have been used for years. But the language should not stand still, and so extra features get added in at each intermediate release level ... and that then gives us the conundrum of what we should provide ...
|2817||Setting a safety net or fallback value in Perl|
Let's say that you want a Perl variable to contain a result which varies depending in the type and values of data fed in. Easy enough - but you need to think about what value you want it to hold if the inputs don't match any of the acceptable values. And there are then two approaches:
1. You can set ...
|2711||For loop - checked once, or evety time? Ruby v Perl comparison and contrast|
Although may aspects of Ruby are inherited (in a non-OO way!) from Perl, there are some distinct differences too; a classic for loop in Perl has its end condition checked every time around the loop, but a Ruby for loop sets up an iterator at the start, so that if something changes within the loop evaluation ...
|657||The ternary operator in Python|
The ? : operator that you may have come across in Perl, PHP, C and Java - known as the ternary or conditional operator - is ABSENT from Python. "But it's so useful" I hear you cry. Ah yes, but isn't this elegant:
val = float(raw_input("Age: "))
status = ("working","retired")[val>65]
print "You should ...
|1825||Question Mark - Colon operator (Perl and PHP)|
The ? and : operator in Perl and PHP allows you to write a single statement that's both an if and an else without the need for all the clutter of keywords, extra variables, and so on if all you want to do is come up with two alternative words.
Here's an example in PHP - throwing a number on a die, and ...
|1696||Saying NOT in Perl, PHP, Python, Lua ...|
"Isn't there one standard way to say NOT?" asked one of my delegates on today's course - and it's an excellent question. But the answer to a question about a negative subject is itself in the negative - no, there isn't just a single way!
In fact .. I can think of no fewer that 12 ways!
• 1. ! ...
|1582||Ruby, C, Java and more - getting out of loops|
break and continue statements have been available for loop control for many years, and others functionallity has been added such as Perl's redo. From today's Ruby Course, here's a table that compares these loop controls in Ruby to similar commands in the other languages that I have been discussing ...
|1220||for loop - how it works (Perl, PHP, Java, C, etc)|
When writing a program, you'll often want to repeat a block of code, counting up through a table or performing a block of code with an input value (loop counter) that goes up 1, 2, 3, 4 etc.
You COULD do this using a while loop, but this means you have to specify each of
• how to start (initialise) ...
|1191||Smart English Output - via PHP and Perl ? : operator|
It's smart to have your program say "there IS in stock" or "there ARE 2 in stock" ... but if you write your code using an "if" and "else" type structure it can become quite verbose.
The ? ... : operator - available in many languages including C, Perl and PHP - allows you to write a single "if,else" ...
|138||Perl - redo and last without a loop|
Perl has next, last and redo commands which (by default) cause you to move on to the next iteration / exit from / repeat the same iteration of a loop.
Did you know that you can also label a block and then use them in a similar way in that block?
$c = 1;
$d = 4;
$e = 5;
redo thyme if ...
Examples from our training material
|andor|| and, or, &&, || - precendence|
|bip|| Different conditionals and comparisons|
|dice|| Sample answer - rolling a die|
|dowhile|| a while loop that runs at least once|
|dunt|| A do ... until loop - reprompt until you get valid input.|
|eloop|| Labelled block style of loop|
|forloop|| Good and bad examples of a for loop|
|g4|| String formatting, conditionals and loops|
|given_demo|| given (Perl 5.10 onwards)|
|golf|| Knockout competition - how many teams / players needed?|
|iffy|| Read - calculate - conditional - output|
|jump2|| Using a label in Perl to jump out of nested loops |
|jumps|| demonstration of next, last and redo|
|jumps2|| Using a label in Perl to jump out of nested loops |
|lazyops|| Lazy and and && operators|
|mls|| Knock out Competition - how many teams?|
|p023.c|| read an array of numbers from a text string|
|power|| inverted until loop example|
|pswit|| given and when - a switchlike syntax|
|qc|| while loop, rewritten as for loop|
|qc2|| use of triadic operator|
|safety_net|| The // defined or operator|
|swit|| Use of label to mimic a switch|
|tae|| Checking user input|
|tel2|| unless, and if and unless written in reverse|
|tel3|| Conditional operators "and" and "or"|
|tel4|| Usin the ? : operator instead of if and else|
|telegram|| if and else example|
|throwz|| Sample answer - throwing a die|
PicturesWe project live demonstrations to help you learn
Customers range from one-man-bands to multinationals
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
The variety that is Perl.
More conditional statements.
If -- single statement rather than a block.
Unless -- an inverted if statement.
The ? : operator.
More loop statements.
The until loop.
Single statement while and until loops.
The for loop.
The do - while loop.
Breaking a loop.
The goto statement.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.