Main Content

Learning to program - Loop statements such as while

Archive - Originally posted on "The Horse's Mouth" - 2014-11-22 10:55:33 - Graham Ellis

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 particular data from each of them (plus perhaps a summary on the end).

while

So all programming languages have the ability to repeat a block of code, and the most fundamentel of these repeats ("loops") looks like this:
  while {some sort of condition is true} then {run a group of statements}

You'll note that this is exactly the same format as the if statement in the previous section, apart from the replacement of the word if by the word while. Operationally, it differs in that once the group of statements in the block has been performed, the program rechecks the condition and if it's still true it runs the block again, keeping doing so until the condition becomes false.

Note

* If the condition is false when first checked, the block isn't performed at all - a loop runs zero or more times
* If the condition never goes false you potentially have an infinite loop that goes on for ever
* Conditions are just the same as the conditions mentioned for the if statement in the language you're learning.

Newcomers to programming sometimes take a few minutes to grasp their first program with a loop statement, as for the first time the code jups backwards as well as forwards as it runs. And they sometimes have trouble working out which statements go where.

* If something may need to be run multiple times, it goes within the block (or within the condition to the while)
* If something only runs once and that's BEFORE the code in the block that may repeat, it goes before the while ("initialisation")
* If something runs once after any repeated code, that goes AFTER the block that may repeat (e.g. printing a total

Your tutor will show you a while loop in the language you're learning at this point, and have you write one too.

breaking out of loops

At times, you'll want to jump out of the middle of a loop and continue running code below - if (for eaxmple) you've identified inoming record that you were looking for from a stream of data, or if you have reached a threshold. So languages provide you with a statement that you can put witin a loop to get out of that loop even though the condition at the top hasn't been checked and gone false. The keyword used is usually break but sometimes (in some languages) it's last.

Putting a break into a loop to be run unconditionally would be rather pointless, so you'll find that any break statements will be within a conditional statement such as i(but not limited to) an if within the loop.

Most languages also support a continue statement (sometimes next) which allows you to skip the rest of the block code and go back up to test the condition straight away. Very useful if you're filtering a stream of data and you've identified a record such as a comment that you want to skip over without further processing.

Some languages have other flow controls in loops to - you may come across redo and retry. Early programming languages supported goto statements (and indeed some still do), but except in exceptional circumstances, their use is discouraged - they make for code that it very difficult to debug or to follow, and often impractical to upgrade when specifications change ... and there are now far better ways.

Other loops

while is the most fundamental loop statement, and it's the natural one for us to choose to introduce on this first day of our "learning to program in ..." course. But there are many other forms of loops that you'll come across later in the course, and in practise you'll find such other loops used more that while. Keyowords are for, foreach, loop, until and repeat.

If you are writing web applications (Java Servlets or JSPs, PHP scripts, or CGI or otherwise server based code in other languages such as Ruby, Python, Tcl, Lua, C or C++) Your whole application will potentially be run in a loop. Each time a user submits a form to your code, (or an automated client calls up your URL), the code is run, in effect in a loop that's controlled by the web server with your code be the innards or the loop. This means that there are times wher code towards the top of your web application may be run after code than appears later in your web application (due to condtionals) even though it doesn't look like it's in a loop at first glance.