There are three basic programming constructs, the two we have done already are:-
Sequence - is the order in which instructions occur and are processed - we learnt this when you started programming and it was the order in which you put your code for it to do exactly what was needed
Selection - determines which path a program takes which it is running - we learnt this with IF statements where the user has options to choose from
The final one is ITERATION this is the repeated execution of a section of code when a program is running
Have a go at this test and see how you get on
Using Iteration - Repetition - FOR NEXT
Have a look at this website and watch the video where iteration is explained
This is an example of an algorithm that has iteration. It is looping around 4 times i.e. 1 to 4.
The i keeps track of how many times it has looped around the program
Make a prediction
What will the output be when this code is executed?
For i = 1 to 4
set P=i*M
output P
Endfor
This is an example of a loop
A for loop is another tool that we have to control the flow of execution in our programs.
We are saying "For every element in this sequence, do this"
i hold the first value (i.e. 1)
P will equal i (1) *M(3)
Output P which is now 1*3 =3
the program doesn't end as it goes around again
i is now 2
P will be 2 *M(3)
Output = 6
This continues until i has done it 4 times
Using this template - Add the code to complete the various tasks.
"" means blank. This could be used to check if a textbox is equal to empty
Mod means to divide
< less than or > greater than
Not IsNumeric - check if it isn't numeric
Length check the length
Try to:-
Add a login box for a user to access the main menu.
Add a login box for two users to access the main menu or using a file system
(e.g. the factors of the number 12 are 6,4,3 and 2 because they divide into 12 exactly).
Hint
to find out whether a number X is a factor of Y use :
IF Y mod X =0 (there is nothing remaining when Y is divided by X)
Extension
Tell the user if the number they entered is a prime number