[Tutor] iteration help

Alan Gauld alan.gauld at btinternet.com
Thu Aug 20 19:16:47 CEST 2015


On 20/08/15 14:27, richard kappler wrote:

> While for and if loops always seem to give me trouble.

A picky point, but it is conceptually very important.

'while' and 'for' are loops - because the loop back
and repeat code.

'if' is not a loop. It is a selector. It only executes
its code once but selects one of several options.

Thee are basically only three(*) concepts in programming
so far as code structure goes:
1) sequence - one instruction after another
2) repetition - code that repeats or loops
3) selection - code that chooses to go down one of many possible paths.

With those three structures you can write any program.
So it is very important that you keep those concepts
separate in your mind. They do very different things.

As to your specific issue I see Joel has given you
some pointers there.

(*)
Some people like to include a fourth: modularity.
The ability to create reusable code blocks such
as functions. But technically it is not needed,
its just a nice to have.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list