[Year 12 SofDev] Re: SD VITTA exam 2, multichoice #1

Kevin Feely feely.kevin.k at edumail.vic.gov.au
Wed Oct 17 09:50:31 EST 2007


Hi All, the following may illustrate the point - taken from 
http://www.tutorials4u.com/c/loopsdecisions.htm
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.

    * This looks at how the flow of execution of the instructions,
      within a program, takes place.
    * The following principals are true to the majority of programming
      languages.
    * Thus learning a second programming language is usually easier than
      learning the first one. You do not have to re-learn these control
      principles.
    * The actual code that is required to execute these principals is
      covered in individual language specific courses.

All programs can be written using these very simple constructs.

   1. *Sequence*
   2. *Repetition (aka iteration or Loops)*
   3. *Conditional Branching*
   4. *Combined Branching and Repetition*

Most programming languages also have more complex constructs that when 
learnt make life easier for the programmer.

Sequence

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.
A sequence of instructions is a list of instructions, which are executed 
in the order in which they are written.
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!

   1. Fill the kettle
   2. Boil water
   3. Coffee in cup
   4. Pour on water
   5. Add sugar
   6. Add milk

Assume that we are now going to make 2 cups of coffee. This could be 
achieved with the following sequence of instructions

.

   1. Fill the kettle
   2. Boil water
   3. Coffee in cup
   4. Pour on water
   5. Add sugar              
   6. Add milk
   7. Coffee in cup
   8. Pour on water
   9. Add sugar              
  10. Add milk   

    * It can clearly be see that instruction 7 - 10, are identical to
      instructions 3 - 6.

Repetition (aka iteration or Loops)

 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.

    * A loop allows the repeated execution of 1 or more instructions.
    * The example below will cater for any *reasonable* number of people.
    * A suggestion of carrying on with the serving of biscuits is
      included. To demonstrate the white space principle to improve the
      readability.

1. Fill the kettle
2. Boil water 
3. Count people requiring coffee:
4. Store answer in a variable CoffeeCount
.
5. *Repeat following steps, value of CoffeeCount number of times* 
*6.         Coffee in cup
7.         Pour on water
8.         Add sugar
9.         Add milk*
*10. End Repeat Construct*
.
11. Count people requiring biscuits etc. .....

    * 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.
    * That the above instructions would also work for a single cup of
      coffee.

Conditional Branching
Selection allows the program to branch and either execute an instruction 
or group of instructions, or not execute those instruction(s).
In our example not everybody will require sugar, so we could refine the 
line to the following

*        Add sugar*

to

*      Do you require sugar?
      If answer is yes
            Add sugar
      End If construct*

    * The instruction "Add sugar" would only be carried out the answer
      to the question Do you require sugar? is yes.
    * The action of adding sugar depends on the result of a Boolean
      condition.
          o Boolean condition results can be one of 2 possible answers.
            The main ones depending on the programming language are:-
                + true or false.
                + 0 or 1.
                      # There are variants to actual numbers used.
                + yes or no.

1. Fill the kettle
2. Boil water 
3. Count people requiring coffee:
4. Store answer in a variable CoffeeCount
 .
*5. Repeat following step, value of CoffeeCount number of times*
*6.         Coffee in cup*
*7. End Repeat Construct*
 .
*8. Repeat following step, value of CoffeeCount number of times*
*9.         Pour on water*
*10. End Repeat Construct
.*
*11. Repeat following steps, value of CoffeeCount number of times*
  .
*12.        Do you require sugar?
13.        If answer is yes
14.                  Add sugar
15.        End If construct*
  .     
*16. End Repeat Construct*
  .
*17. Repeat following step, value of CoffeeCount number of times*     
*18.        Add milk*
*19. End Repeat Construct*
 .
20. Count people requiring biscuits etc. .....

Notes

    * Step 12 is the condition, which is tested in step 13.
    * The control start (step 13) and end (step 15) of the Conditional
      Branching construct
    *
    * For each start there should be a corresponding end.

 > 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.

Combined Conditional testing and Repetition

Alternatives:-

    * Some conditional branching constructs combine with a loop construct.
    * The conditional test may be at the start or at the end.
    *

      Conditional test at start

1. 
   Some condition
*2. Repeat while the condition is true
3.       Code to be repeated
4.       Some condition
5. End Repeat Construct*

    * Step 1. This step is only executed once, the white space can be
      placed in front of the "Some condition" or after it.
    * The loop will *not* be executed if the condition is false.
    * The loop will repeat while ever the condition remains true.
          o This creates a problem --- How to end the loop.
          o 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 :-
                + Incrementing / decrementing the value of a variable
                  used in the condition.
                + Obtaining user input, that will alter the value of a
                  variable in the condition.
    * The additional same condition, step 4, within the loop, is for the
      following test, step 2.
    *

    * Conditional test at the end the construct, e.g.

*1. Start loop*
*2.       Code to be repeated
3.       Some condition
4. Repeat while the condition is true*

    * The loop will always be executed at least once, think what you
      could do if this is a problem.
    * The loop will repeat while ever the condition remains true. See
      above for preventing problems.


      Programming languages.

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:-

if, then, else, while, repeat, until, with, do, for, loop, true, false, 
switch, case, default, end, exit, continue, break

This example is provide from John McGuinns programming tutorial and is 
supplied for demonstration only - not to be reproduced for commercial gain

regards
Kev

Important - 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20071017/7bfef6dd/attachment.html


More information about the sofdev mailing list