<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<p>Hi All, the following may illustrate the point - taken from
<a class="moz-txt-link-freetext" href="http://www.tutorials4u.com/c/loopsdecisions.htm">http://www.tutorials4u.com/c/loopsdecisions.htm</a><br>
A Fill the kettle. This Step can be broken down into many sub steps.
Fill through spout, or lift lid and fill. Take kettle to sink. Place
under tap. Turn on tap. Etc. etc. </p>
<ul>
<li>This looks at how the flow of execution of the instructions,
within a program, takes place.</li>
<li>The following principals are true to the majority of programming
languages.</li>
<li>Thus learning a second programming language is usually easier
than learning the first one. You do not have to re-learn these control
principles. </li>
<li>The actual code that is required to execute these principals is
covered in individual language specific courses.</li>
</ul>
<p> All programs can be written using these very simple constructs.</p>
<ol>
<li><b>Sequence</b></li>
<li><b>Repetition (aka iteration or Loops)</b></li>
<li><b>Conditional Branching</b></li>
<li><b>Combined Branching and Repetition</b></li>
</ol>
<p> Most programming languages also have more complex constructs that
when learnt make life easier for the programmer.<br>
</p>
<p>Sequence</p>
When you start to learn programming, the initial programs are very
simple and usually consist of a sequence of instructions that are
executed one after the other. <br>
A sequence of instructions is a list of instructions, which are
executed in the order in which they are written. <br>
I will illustrate this using a example of writing a few simple
instructions for making a cup of instant coffee that includes sugar and
milk!
<ol>
<li>Fill the kettle </li>
<li>Boil water </li>
<li>Coffee in cup </li>
<li>Pour on water </li>
<li>Add sugar</li>
<li>Add milk</li>
</ol>
<p> Assume that we are now going to make 2 cups of coffee. This could
be achieved with the following sequence of instructions<br>
</p>
<p> .</p>
<ol>
<li>Fill the kettle</li>
<li>Boil water </li>
<li>Coffee in cup</li>
<li>Pour on water</li>
<li>Add sugar </li>
<li>Add milk </li>
<li>Coffee in cup</li>
<li>Pour on water</li>
<li>Add sugar </li>
<li>Add milk </li>
</ol>
<ul>
<li>It can clearly be see that instruction 7 - 10, are identical to
instructions 3 - 6.</li>
</ul>
Repetition (aka iteration or Loops)
<p> From the example above a better method is to introduce an
instruction that will iterate though some of our previous sequential
instructions, and create a loop of instructions.</p>
<ul>
<li> A loop allows the repeated execution of 1 or more instructions.</li>
<li>The example below will cater for any <b>reasonable</b> number of
people.</li>
<li>A suggestion of carrying on with the serving of biscuits is
included. To demonstrate the white space principle to improve the
readability.</li>
</ul>
<pre>1. Fill the kettle
2. Boil water
3. Count people requiring coffee:
4. Store answer in a variable CoffeeCount
.
5. <font color="#000080"><b>Repeat following steps, value of CoffeeCount number of times</b></font>
<b>6. Coffee in cup
7. Pour on water
8. Add sugar
9. Add milk</b>
<b><font color="#000080">10. End Repeat Construct</font></b>
.
11. Count people requiring biscuits etc. .....</pre>
<ul>
<li>A variable, CoffeeCount, is used, steps 4 and 5 the variable .
Step 4 would obtain the value, 2 in the example. Then step 5 would use
this value, 2, and repeat the loop twice.</li>
<li> That the above instructions would also work for a single cup of
coffee. </li>
</ul>
Conditional Branching
<br>
Selection allows the program to branch and either execute an
instruction or
group of instructions, or not execute those instruction(s).
<br>
In our example not everybody will require sugar, so we could refine the
line to
the following
<pre><b> Add sugar</b></pre>
<p>
to</p>
<pre><b><font color="#000080"> Do you require sugar?
If answer is yes</font>
Add sugar
<font color="#000080">End If construct</font></b></pre>
<ul>
<li>The instruction "Add sugar" would only be carried out the answer
to the question Do you require sugar? is yes. </li>
<li>The action of adding sugar depends on the result of a Boolean
condition.
<ul>
<li>Boolean condition results can be one of 2 possible answers.
The main ones depending on the programming language are:-
<ul>
<li>true or false.</li>
<li>0 or 1.
<ul>
<li>There are variants to actual numbers used.</li>
</ul>
</li>
<li>yes or no. </li>
</ul>
</li>
</ul>
</li>
</ul>
<pre>1. Fill the kettle
2. Boil water
3. Count people requiring coffee:
4. Store answer in a variable CoffeeCount
.
<font color="#000080"><b>5. Repeat following step, value of CoffeeCount number of times</b></font>
<b>6. Coffee in cup</b>
<b><font color="#000080">7. End Repeat Construct</font></b>
.
<font color="#000080"><b>8. Repeat following step, value of CoffeeCount number of times</b></font>
<b>9. Pour on water</b>
<b><font color="#000080">10. End Repeat Construct
.</font></b>
<font color="#000080"><b>11. Repeat following steps, value of CoffeeCount number of times</b></font>
.
<b><font color="#ff0000">12. Do you require sugar?</font>
<font color="#800040">13. If answer is yes</font>
14. Add sugar
<font color="#000080"><font color="#800040">15. End If construct</font></font></b>
.
<b><font color="#000080">16. End Repeat Construct</font></b>
.
<font color="#000080"><b>17. Repeat following step, value of CoffeeCount number of times</b></font>
<b>18. Add milk</b>
<b><font color="#000080">19. End Repeat Construct</font></b>
.
20. Count people requiring biscuits etc. .....</pre>
<p>
Notes </p>
<ul>
<li><font color="#ff0000">Step 12 is the condition,</font> which is
<font color="#800040">tested in step 13.</font></li>
<li>The control start (step 13) and end (step 15) of the Conditional
Branching
construct</li>
<li> </li>
<li>For each start there should be a corresponding end. </li>
</ul>
<p>
> Care must be taken if the answer is 0 (zero) to the question how
many people
require a cup of coffee because some loop instructions will always
execute the
loop at least once, and this may be an undesirable result. This depends
on the
programming language used, and the actual instruction used. Look for
information on this possible problem. And remember to test loops with 0
to
ensure that you do not receive unexpected results.<br>
</p>
<p>Combined Conditional testing and Repetition</p>
<p>
Alternatives:-</p>
<ul>
<li>Some conditional branching constructs combine with a loop
construct.</li>
<li>The conditional test may be at the start or at the end.</li>
<li><br>
Conditional test at start</li>
</ul>
<pre>1.
Some condition
<b><font color="#000080">2. Repeat while the condition is true</font>
3. Code to be repeated
4. Some condition
<font color="#000080">5. End Repeat Construct</font></b></pre>
<ul>
<li>Step 1. This step is only executed once, the white space can be
placed in
front of the "Some condition" or after it. </li>
<li>The loop will <b>not</b> be executed if the condition is false. </li>
<li>The loop will repeat while ever the condition remains true.
<ul>
<li>This creates a problem --- How to end the loop.</li>
<li>Additional code is required within the loop that allows the
condition
result to change. So that the loop will terminate. 2 possible ways of
doing
this :-
<ul>
<li>Incrementing / decrementing the value of a variable used
in the condition. </li>
<li>Obtaining user input, that will alter the value of a
variable in the condition.</li>
</ul>
</li>
</ul>
</li>
<li>The additional same condition, step 4, within the loop, is for
the
following test, step 2.</li>
<li><br>
</li>
<li>Conditional test at the end the construct, e.g. </li>
</ul>
<pre><b><font color="#0000a0">1. Start loop</font></b>
<b><font color="#000080">2. </font> Code to be repeated
3. Some condition
<font color="#000080">4. Repeat while the condition is true</font></b>
</pre>
<ul>
<li>The loop will always be executed at least once, think what you
could do if
this is a problem.</li>
<li>The loop will repeat while ever the condition remains true. See
above for
preventing problems.<br>
<br>
<br>
Programming languages.</li>
</ul>
<p>
The main words, words with the following as a prefix, or part of words,
to look
out for in the various programming languages, that are connected to
these
control structures are:-</p>
<p>
if, then, else, while, repeat, until, with, do, for, loop, true, false,
switch,
case, default, end, exit, continue, break </p>
This example is provide from John McGuinns programming tutorial and is
supplied for demonstration only - not to be reproduced for commercial
gain<br>
<br>
regards<br>
Kev<br>
<p></p><p><b>Important - </b>This email and any attachments may be confidential. If received in error, please contact us and delete all copies. Before opening or using attachments check them for viruses and defects. Regardless of any loss, damage or consequence, whether caused by the negligence of the sender or not, resulting directly or indirectly from the use of any attached files our liability is limited to resupplying any affected attachments. Any representations or opinions expressed are those of the individual sender, and not necessarily those of the Department of Education and Early Childhood Development.</p></body>
</html>