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
Lua module U103
Conditionals and loops
Exercises, examples and other material relating to training module U103. This topic is presented on public courses Learning to program in Lua
, Lua Programming
|Articles and tips on this subject||updated|
|3397||Does 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 ...
|3686||The goto statement in Lua|
In the past there was considerable debate in academia and industry on the merits of the use of goto statements, and indeed Lua didn't have them until release 5.2. Well used in Lua, they can provide an escape from inner loops, jumping to a label using a ::label:: format. For example:
|3558||Python or Lua - which should I use / learn?|
"Should I learn and use Python or Lua" - a question to me. The questioner was perceptive, knowing that he shouldn't try to learn lots of languages at the same time, and that he should always limit the "basket" of languages used within his applications - in fact, he was wondering if he could select just ...
|2454||For loops in Lua|
As an old Fortran programmer, I remember that a for loop was given a start point, an end point and a step ... and they had to be whole numbers (integers). Lua's for loops can be used that way, but they can be used a lot more flexibly too.
From, to, step for loops
Th egood news is that Lua works in ...
|2455||Lua examples - coroutines, error handling, objects, etc|
I have presented a bespoke Lua course over the last three days ... and it has lead me to some interesting new examples which I'm sharing here.
Object Orientation is more a way of thinking and coding than a language feature in Lua - when you set up a piece of data / structure / object, you do so in a ...
|2351||Ternary 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 ...
|1738||Clean code, jump free (Example in Lua)|
The "goto" statement - in languages that still support it - is regarded with disdain by Computer Scientists as it makes for spaghetti code - hard to follow and going all over the place!
In some ways, loop controls "break" and "continue" ("last", "next" and "redo" in Perl; "next", "redo", "retry" 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. ! ...
|1685||Short circuit evaluation (lazy operator) in Lua|
In Lua, if a partial expression evaluates to true the following section will NOT be evaluated if it follows an or because the whole thing cannot be true.
If a partial expression evaluates to false, the following section will NOT be evaluated if it follow an and because the whole thing must be true.
Examples from our training material
|andor|| Lazy and / or operators|
|bpm|| string.find v string.gfind|
|clike|| for loop with pairs and ipairs|
|clumpit|| for loop termination|
|doco|| Changing loop counts|
|exa|| Loops and conditionals - exchange rate example|
|floo|| for loop in which end condition changes|
|kz|| Example of a for loop|
|lif|| if, elseif, else|
|ll2|| while loop and repeat until loop|
|lloops|| while and for loops|
|lsx|| Lua for loop - termination|
|swans.lua|| Swan family - Lua|
|tchart|| for loop with start, end and step|
|wcd|| Comparison of while and until loops|
|whilly|| Writing cleaner code without break or continue|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from http://www.training-notes.co.uk
Topics covered in this module
If while repeat break do and for.
Comparing numbers comparing strings.
Equality and nil.
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. Also
available is the Opentalk
Forum for discussion of technical questions.