Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
While, for, foreach or something else to loop.

Newcomers to programming often ask "which loop should I use?"

Every language has a while loop, most have a for loop (though sometimes those differ in what they do - Python is an exceptional one, for example), and many have a foreach loop. Then you get the odd ones such as the until loop in Perl ...

A while loop is at the "lowest level" - you can always do what you want with that, but it may be a bit wordier than you wish in coding, and that may make it a bit more prone to coding with errors ("bugs"), and it may make it harder for someone coming along later to update your code to understand what's going on.

On yesterday's PHP course, I took an example of an array that I wanted to print our element by element ...

Using a while loop, I have separate statements to set the position number in the array that I'm starting at, to check the end, and to step up the counter (to the array) by one:

  $at = 0;
  while ($at < count($info)) {
    print "w: $info[$at]\n";
    $at++;
    }


With a for loop, I take my initial position setter and my stepper and move them into the controlling for statement, making the code much easier to follow, but not really reducing the code elements:

  for ($at = 0; $at < count($info); $at++) {
    print "f: $info[$at]\n";
    }


If I don't need to know the position number (don't want to print it out, or look back to the "previous", forward to the "next", then I can do completely without the position variable (called $at in my example) using a foreach loop:

  foreach ($info as $fruit) {
    print "g: $fruit\n";
    }


Complete example - [here]. PHP courses - [here].
(written 2012-11-06, updated 2012-11-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P206 - Perl - More Loops and Conditionals
  [138] Perl - redo and last without a loop - (2004-12-02)
  [299] What - no switch or case statement? - (2005-05-03)
  [657] The ternary operator in Python - (2006-03-25)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2815] switch and case, or given and when in Perl - (2010-06-17)
  [2817] Setting a safety net or fallback value in Perl - (2010-06-19)
  [2824] A pint of Black Rat, and a lazy barman - (2010-06-25)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
  [2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [3619] Ruby v Perl - a comparison example - (2012-02-21)

H104 - PHP - Control Statements
  [340] Code and code maintainance efficiency - (2005-06-08)
  [353] Wimbledon Neck - (2005-06-20)
  [406] Assignment, equality and identity in PHP - (2005-08-08)
  [421] Don't repeat code - use loops or functions - (2005-08-21)
  [863] Double and Triple equals operator in PHP - (2006-09-12)
  [1199] Testing for one of a list of values. - (2007-05-22)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
  [2912] Predictions for the seagull population - (2010-08-09)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)


Back to
How many times ... has this loco headed west through Tenby? - Python exceptions
Previous and next
or
Horse's mouth home
Forward to
How does PHP work?
Some other Articles
Multiple page web applications - maintaining state - PHP
BODMAS - the order a computer evaluates arithmetic expressions
PHP variables - dynamically typed. What does that mean?
How does PHP work?
While, for, foreach or something else to loop.
How many times ... has this loco headed west through Tenby? - Python exceptions
Sand to Arabia, Coals to Newcastle or Woodburners to Russia
How well do you know Perl and / or Python?
Identifying your real customers and keeping them well informed fast
Every Customer Counts
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., 2021: 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/mouth/3914_Whi ... loop-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb