Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - 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 who provide Open Source Training Courses and business 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
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Perl module P204
Conditionals and Loops
Exercises, examples and other material relating to training module P204. This topic is presented on public courses Perl Programming, Perl bootcamp, Learning to program in Perl, Perl Programming

In Perl, if and while statements (and many others too) are used to control a block of code that may run many times, or may run only if a certain condition holds. This module not only introduces you to if and while, but also gives you some programming tips to help you come to grips with the increasingly complex structure that you'll be writing as your program gets longer.


Articles and tips on this subjectupdated
4323Learning to program - Loop statements such as while
If your program always ran each statement just once (indeed skipping over statements which were in blocks in false conditions) it would run very quickly and would have little use. You couldn't (for example) run a program which went through a whole series of results from a database query and displayed ...
2014-11-22
 
4322Learning to Program - the conditional statement (if)
Every language has some sort of conditional statement. That's a way of looking at some sort of setting or status in the program and performing some sort of action based on that setting or status. such statements take the form ...   if {some sort of condition is true} then {run a group of ...
2014-11-22
(longer)
4031Showing what programming errors look like - web site pitfall
One of the elememts of a programming training course is showing people what happens when something goes wrong. We all make "silly" coding mistakes from time to time, and these often result in a syntax error when we compile (C, C++, Java), start to run (Perl, PHP, Python, Ruby, Lua) or reach the statement ...
2013-03-06
(longer)
3895Flowchart to program - learning to program with Well House
Drawing a flowchart is how many people understand a process; it's something that's been used in schools and textbooks for many generations, and it provides a graphic representation of choices and actions that most people find easy to follow. For newcomers to programming, such as on my Learning to program ...
2012-10-20
 
3397Does a for loop evaluate its end condition once, or on every iteration?
All the languages that we teach have a for loop or the equivalent, which is a clean way of repeating a block of code with a rising or falling index number. It's used in many circumstances - for example in iterating through the months of the year (for m goes from 1 to 12) of in stepping through all ...
2012-04-15
 
3004Increment operators for counting - Perl, PHP, C and others
Look at this Perl statement:   $counter = $counter + 1; "Take the value in $counter, add 1 to it, and put it back in $counter". It's a common programing requirement - indeed, so common that you can write it in a shorter form in many languages:   $counter += 1; "Add one to the value ...
2010-10-20
 
2832Are 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 ...
2010-06-27
(longer)
2711For 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 ...
2010-04-08
 
2550Do not copy and paste code - there are much better ways
If you find yourself using copy and paste to duplicate a piece of code, take a step back and think again. You're doubling your maintenance work, with two separate sets of code to look after and keep in step from that day forwards. Are you going to give me the "but I have to change one copy a bit" excuse? ...
2010-01-06
 
2351Ternary operators alternatives - Perl and Lua lazy operators
"If the gender is male, the answer is him, but if the gender is female, the answer is her". A common situation in programming - [is/are], [him/her], [yes/no], [child/children], [ice/water/steam], [public/private] - and C and Perl and PHP provide the "ternary" operator ? and : to provide a shorthand ...
2009-08-12
 
1727Equality and looks like tests - Perl
Whenever you do an equality check in a Perl program, you must think whether you're checking if two numbers are equal, if two test strings are equal, or if a string looks like a pattern. And you write different code in each case: Checking numbers: If ($stuff == 6) { ... Tests whether $stuff contains ...
2008-07-29
 
1696Saying 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. ! ...
2008-07-05
 
1607Learning to program in Perl
For the last few days, I've been teaching Learning to Program in Perl. Unlike a conventional course style, for this one I had been asked to assume no prior programming knowledge, and covered first principles such as variables, conditionals, and loops. Here is one of the examples I wrote during the course ...
2008-04-12
 
1477Decisions - small ones, or big ones?
When you're traveling, you'll sometimes come to a point at which you make a decision - to go one way or to go another. Sometimes the decision is a small one - for example, if I'm driving through Marlborough there's a choice of the town centre or the road around the back; whichever I take, they come ...
2007-12-19
 
1468Lexical v Arithemetic testing, Bash and Perl
If you tell a story against someone, best to be telling it against yourself! One of our web servers (running standard, not our own software behind the scenes) has been having a problem with handling denial of service attacks which are coming in from time to time ... and I have a monitoring script running ...
2007-12-12
 
1191Smart 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" ...
2007-05-18
 
930-> , >= and => in Perl
Yes, Perl is eclectic - lots of things it can do, taken from lots of places and so, yes, it does have all three of those operators! -> Run the method named to the right on the object named on the left >= Return 1 (a true value) if the expression to the left is numerically greater than the expression ...
2006-11-18
 
353Wimbledon Neck
What use is the "+=" operator? Why did the language designers bother to provide it? Yet they did ... In Perl and PHP $number_on_bus = $number_on_bus + $goton_here; becomes $number_on_bus += $goton_here; In Python number_on_bus = number_on_bus + goton_here becomes number_on_bus += goton_here In ...
2006-06-05
 
Examples from our training material
ask   if, elsif and else (right)
bitwise   Demonstration of bitwise and logical operators
bmi3   Demonstration with conditionals
bmi4   bmi demonstration with loops
condition   if and if - else; blocks.
d2r1   Illustration of looping to process a series of values
d2r2   Illustration of looping to process a series of values, accumulating totals
din   Use of shorthand operators
dinners   nested blocks - loops and conditionals
looper   while loop
lp1   Blocks within blocks
lp2   Blocks within blocks (shows \ protection)
nask   if, elsif and else (wrong use of =)
numbers   how to write numeric constants
readandtotal   Reading and totalling numbers - sample answer
summer   Answer to practical (reading and summing numbers)
Pictures
Getting to grips with Perl
Background information
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 "if" statement.
Structure.
Conditions.
Blocks.
The "while" statement.
Shorthand operators.
Add and assign.
Increment.
Multiple assign.
String expansion.
Assignments within other statements.
More on the increment operator.
Ways of writing numbers.
Summary.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Ruby, Lua, Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses 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.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/P204.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb