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
 
Breaking the running sequence - an introduction to conditional statements and loops

A program is a series of instructions which are run in sequestially - or so you'll be told when you start programming. And, indeed, that's the default - the way it is unless you write code to do something different. "A program is a series of statemnets each separated by [something] which run one after another".

Old cottages in CorshamBut ... there are going to be times that you only want someting to be done in certain conditions. Do you say to a guest in your low-ceilinged cottage "mind your head"? You only want to do so if your guests is fairly tall (in fact, on last week's C and C++ course we decided that you do NOT need to tell very tall guests as they'll be used to ducking everywhere in their daily life!), but you don't want to insult people who are really short. So - in our code - we put a conditional statment of the form:
  if (condtion) {things to do only if true}

The example we wrote - full source code, a longer decsription that's specific to C++ and C, and sample output may be found [here].


Santa at Melksham, December 06There are time, too, where you want to keep repeating statements - "do that again" - and typically a whole series of statements, until a certain condition is met. For example, if you've eaten rather well in the run up to Christmas, and perhaps over Christmas as well, you may with to loose 2kg and see if you're healthier. Well - yes, you're healthier, but you could do with loosing anothe couple of kgs, and another couple, and so on until you reach an ideal body mass index ... and in coding terms, that's a loop. We write it of the form:
  while (condtion) {things to do only if true}
which (you'll note) is almost identical to my if statement. The difference is that when cose like this is run, the program goes back and tests the condition again once it's completed the running of the block the first time ... and it keeps doing so while the condition remains true.

The example we wrote - full source code, a longer decsription that's specific to C++ and C, and sample output may be found [here].

---OOO---


If you find your self repeating the same thing a number of times one after another in your code, you've probably got it wrong - you should be using a loop. As well as while loops, C and C++ support for loops which offer a start point - step - end point capability so that you can say "go from 1 to 12 in steps or 1" all in a single statement (and they can do much more too). In other languages, you'll find other loop statements such as loop, foreach and until.






(written 2011-04-11)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C203 - C and C based languages - Conditionals and Loops
  [4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3384] Loops - a comparison of goto, while and for - (2011-08-10)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3121] New year, new C Course - (2011-01-05)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2570] Function Prototypes in C - (2010-01-11)
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [353] Wimbledon Neck - (2005-06-20)


Back to
How to return 2 values from a function (C++ and C) - more uses of pointers
Previous and next
or
Horse's mouth home
Forward to
C and C++ - preprocess, compile, load, run - what each step is for
Some other Articles
Light and dark at Green Park
Melksham - the way forward. 26th April, Well House Manor
Collections in C and C++ - arrays, vectors and heap memory blocks
C and C++ - preprocess, compile, load, run - what each step is for
Breaking the running sequence - an introduction to conditional statements and loops
How to return 2 values from a function (C++ and C) - more uses of pointers
Spring in the countryside near Melksham
Melksham Town Council - vacancy in the Spa Ward
TrainWest, 2011 in pictures - Christie Miller, Bowerhill, Melksham
Bradshaw, Ben and Bill. And some C and C++ pointers and references too.
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/3243_Bre ... loops.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat