Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Learning 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 statements}

if

The word if applies in every language that we teach at Well House Consultants at present, but how we define the condition, how we signify the end of the condition and the start of the group of statements, and how we stop and start that group varies.

Some definitions
- a BLOCK of code is a series of statements grouped together. Actually zero or more statements, as at times you'll want to have a "do nothing" group
- DELIMITERS are the characters or character groupings thet start and end blocks. They may be the words then and , they may be the characters { and }, or thy may be a pattern of spaces and tabs that insets the block in the source code. Sometimes they may be left out, and if the language supports that they imply a block of a single statement.

The condition that's used in an if statement if going to be an expression that evaluates to a "yes" or "no" value - true or false. Exactly what comprises true and false varies between languages - very often if your conditional expression works out at zero, that's false and it it works out to any other number, that's true. But numbers are only used in this way a small proportion of the time, as languages come with special oeprators which compare two values and return true or false based on that comparison. Commonly they are:
  == ... "is equal to"
  != ... "is not equal to"
  < ... "is less than"
  <= ... "is less than or equal to"
  > ... "is greater than"
  >= ... "is greater than or equal to"

But BEWARE ... in some languages (SQL and Lua) even these vary, and in many languages (Perl, PHP, Shell for example) there are alternatives which do somewhat different things, and in some (Java, Python, Ruby, SQL, C, C++ for example) there are functions (I'lltell you about those another time) which you may call to make alternative comparisons. In other words, you're never limited to just the six comarisons.

These are notes to accompany your "Learning to program in xxxxx" course at Well House Consultants, so I'm not going to attempt to describe all the options here. Instead, your tutor will demonstrate the first, most basic conditional statements to you at this point and let you try them out.

One of the question that comes up for newcomers to programming is "why do I need a closing delimiter?". It's needed to tell your program that you've reached then end of code that's only run in certain circumstances and you're back into "always" code beyond that point. Imagine that you're driving a car and you decide to pull into a service station:
  if (ineed == "loo") then { ......}
The block of code in the curly braces defines what you need to do in the service station, but then when you get backon the main road it's "carry on as before", even if one or two of the variables (such as your comfort level) have been amended. The closure is vital as it removes the need for all subsequent code to be repeated - it's an indication of the coming together.

elseif and else

Usually, you'll want to perform one action if a condition is true, and some different action if the condition is false. Whilst it would be possible for you to write the "opposite" if statement, that's ineffiecent (at writing and run time) and prone to error, so languages support some sort of otherwise statement.

You can follow an if with one or more elseif or elsif or elif clauses (the keyword varies from language to language!) which in each case will have a further condition attached to them, and they'll have a block of code that runs if that alternative condition is true.

Note that the order of the various conditions is important, as once a true condition is found as the code runs, that's the block that will be run and the following ones won't be, even if the condition on them is also true.

Finally, you may finish your if statement with else and a block to accompany it. This is your 'catch all' or safety net which will be performed if neither the if condition, nor any of the el[se]if conditions was true. The else is optional, you can only have one of them, and there is no condition attached to it.

nested and joined conditions

You'll often find that you want to test for conditions within conditions (i.e. within the block of what to do) and you can do this. If you've stopped at the service area above because you needed a natural break, you'll be making other subsidiary decisions in there about wheth to use the loo, have a coffee, buy sweets for the kids, call your desination to update your arrival time, etc. Note that you'll complete all of those extra actions before you complete the main action of making a stop at the service area ... So nested conditional start in the order 1, 2, 3 but end 3, 2, 1.

There are also times that you want to perform a certain action only if two conditions are true. You could do this with nested blocks, but you'll also have an alternative, typically using the words and or or to link up conditions into a single composite conditions. Very often, either && or & are alternatives (with subtle differences) for and, and or may be relplaced by || or |. This is a subject for much deeper study later in the course.

testing

As soon as you start introducing condtional code, you introduce multiple routes through the code so it becomes very important to give throught to a thorough testing regime.

As a minimum, you should test your progran before its use in a live application by running every single possible condition though its true and false routes. And you should consider also:
* Running your code such that all combinations of conditions are tested
* Testing your data where both valid and invalid user inputs are made
* Remember to test "boundary" conditions ... if you're testing for age under 18, run your code with (say) 16 and 20 ... BUT ALSO with 18 itself.

Testing gets to be repetitive and (let's admit it) a bit boring at times, and it's far too easy for us to skip. Yet it really should be repeated in full for each and every iteration / release of the code. We'll broach the detail of testing later on the course, but for the moment bear in mind that a standard set of tests, automated in a file so that you can easily rerun them, and with extra software to pick up hundreds or thousands of passes and the occasional fail is going to be far better that your programmer working through each and every test at every upgrade. You might even want to write the tests before you write the code that it's going to be testing - that's Test Driven Development or TDD
(written 2014-11-21, updated 2014-11-22)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q100 - Object Orientation and General technical topics - Learning to Progam
  [4575] Learning not just what a program does, but how to design it in the first place. - (2015-11-06)
  [4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
  [4324] Learning to program - variables and constants - (2014-11-22)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4318] Learning to Program - how we start to teach you at Well House Consultants - (2014-11-16)
  [4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
  [2973] Learning to program - where to start if you have never programmed before - (2010-09-28)
  [2898] Programming Standards from the start! - (2010-08-02)
  [2505] I almost put the bins out this morning - (2009-11-16)
  [2504] Learning to program in ... - (2009-11-15)
  [2326] Learn a new programming language this summer. - (2009-08-06)
  [2294] Can you learn to program in 4 days? - (2009-07-16)
  [2286] New to programming? It is natural (but needless) for you to be nervous - (2009-07-14)
  [2092] Tracking difficult bugs, the programmer / customer relationship - (2009-03-20)
  [2048] Learning to program in PHP, Python, Java or Lua ... - (2009-02-19)
  [2001] I have not programmed before, and need to learn - (2009-01-19)
  [1985] Learning to program as a part of your job - (2009-01-10)
  [1963] Best source to learn Java (or Perl or PHP or Python) - (2008-12-28)
  [1605] Learning and understanding scripting programming techniques - (2008-04-08)
  [116] The next generation of programmer - (2004-11-13)

H104 - PHP - Control Statements
  [3914] While, for, foreach or something else to loop. - (2012-11-06)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [2912] Predictions for the seagull population - (2010-08-09)
  [2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
  [1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1199] Testing for one of a list of values. - (2007-05-22)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [863] Double and Triple equals operator in PHP - (2006-09-12)
  [657] The ternary operator in Python - (2006-03-25)
  [421] Don't repeat code - use loops or functions - (2005-08-21)
  [406] Assignment, equality and identity in PHP - (2005-08-08)
  [353] Wimbledon Neck - (2005-06-20)
  [340] Code and code maintainance efficiency - (2005-06-08)

Y103 - Python - Conditionals and Loops
  [4723] Conditional operators in Python - (2016-11-05)
  [4713] Equality (in Python) - (2016-10-30)
  [4545] Method, Class, Module, Package - how to they relate in Python? - (2015-10-17)
  [4541] Setting up and tearing down with the Python with keyword - (2015-10-16)
  [4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
  [4210] If elif elif elif - multiway selection in Python - (2013-11-16)
  [4092] Identity in Python - (2013-05-17)
  [3762] Learning to program - the if statement. Python. - (2012-06-12)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [1661] Equality, sameness and identity - Python - (2008-05-31)
  [1201] No switch in Python - (2007-05-23)
  [909] Python is like a narrowboat - (2006-10-30)
  [835] Python - when to use the in operator - (2006-08-16)
  [788] New - Conditional expressions in Python 2.5 - (2006-07-01)
  [668] Python - block insets help with documentation - (2006-04-04)
  [299] What - no switch or case statement? - (2005-05-03)

U103 - Lua - Conditionals and loops
  [4574] repeat until in Lua - a one or more rather than a zero or more loop - (2015-11-05)
  [4272] Lazy operators in Lua - what they mean, and examples - (2014-05-05)
  [3686] The goto statement in Lua - (2012-04-06)
  [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
  [2454] For loops in Lua - (2009-10-14)
  [2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
  [1738] Clean code, jump free (Example in Lua) - (2008-08-06)
  [1685] Short circuit evaluation (lazy operator) in Lua - (2008-06-22)

P204 - Perl - Conditionals and Loops
  [4031] Showing what programming errors look like - web site pitfall - (2013-03-06)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [1727] Equality and looks like tests - Perl - (2008-07-29)
  [1607] Learning to program in Perl - (2008-04-11)
  [1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
  [930] -> , >= and => in Perl - (2006-11-18)

T203 - Tcl/Tk - Conditionals and Loops
  [4455] Working out distance between places, using OS grid references and a program in Tcl - (2015-03-11)
  [3571] Comparing loop commands in Tcl - (2012-01-06)
  [3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
  [3189] Tcl - the danger of square brackets in a while command - (2011-03-02)
  [2681] Tcl - a great engineering language - (2010-03-17)
  [2471] A short form of if ... then ... else - (2009-10-23)
  [2261] Tcl - nice and nasty - (2009-06-29)
  [1401] Tcl - using [] or {} for conditions in an if (and while) - (2007-10-23)
  [210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)

C203 - C and C based languages - Conditionals and Loops
  [3384] Loops - a comparison of goto, while and for - (2011-08-10)
  [3243] Breaking the running sequence - an introduction to conditional statements and loops - (2011-04-11)
  [3121] New year, new C Course - (2011-01-05)
  [2570] Function Prototypes in C - (2010-01-11)
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)

R104 - Ruby - Control Structures
  [4674] Alternating valuses / flip-flop / toggle - example in Ruby - (2016-05-17)
  [4504] Where does Ruby load modules from, and how to load from current directory - (2015-06-03)
  [4503] Separating your code for easier testing, understanding and re-use; example in Ruby - (2015-06-02)
  [4370] Conditionals, loops and methods in Ruby - a primer with simple examples - (2014-12-29)
  [3769] Muttable v immutable and implications - Ruby - (2012-06-20)
  [3619] Ruby v Perl - a comparison example - (2012-02-21)
  [3422] Assigning values to variables within other statements - Ruby - (2011-09-07)
  [3254] Multiple inputs, multiple out, ruby functions - (2011-04-19)
  [3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
  [3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
  [3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
  [3156] Splitting data reading code from data processing code - Ruby - (2011-02-04)
  [2975] Why do I need brackets in Ruby ... or Perl, Python, C or Java - (2010-09-29)
  [2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
  [2619] Passing code to procedures and yield in Ruby - (2010-02-02)
  [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
  [1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
  [1891] Ruby to access web services - (2008-11-16)
  [1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
  [1870] What to do with a huge crop of apples - (2008-11-04)
  [1587] Some Ruby programming examples from our course - (2008-03-21)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [995] Ruby's case - no break - (2006-12-17)
  [985] Equality in Ruby - == eql? and equal? - (2006-12-14)
  [960] 1st, 2nd, 3rd revisited in Ruby - (2006-12-02)

J704 - Java - Loops and Conditional Statements
  [4427] Java example - for loop and conditionals from course exercise - (2015-02-16)
  [4346] A behaviour driven example of writing a Java program - (2014-12-09)
  [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
  [2734] for and foreach in Java - (2010-04-22)


Back to
Are administration / review charges on hotel guests acceptable?
Previous and next
or
Horse's mouth home
Forward to
Learning to program - Loop statements such as while
Some other Articles
Learning to Program - the conditional statement (if)
Are administration / review charges on hotel guests acceptable?
An example of Model-View-Controller techniques in a Perl / CGI script
PHP - some quick fixes if scripts have issues, and how to use our support
Java - an update of the basics
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4322_Lea ... t-if-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat