In programming it is very common that a list of elements needs to be iterated (repeatedly called).
There is only one problem with this example: every time the list abc is called it needs to be recalculated.
In many cases, this makes no difference. However, if the list is sizeable, this could lead to appreciable delays.
It is therefore a good practice to avoid this inconvenience - and this is not hard to do: simply store the length of the list prior to starting the loop:
It might seem obvious to use a
for... in loop?
However, this would be committing a serious error. The
for... in loop does not iterate only the properties of an abject, but also all of its numeratable properties - that is, also those inherited from the prototype.
We must therefore ensure that the properties passed to the loop do not originate from the prototype. For this purpose, we use the
if statement is probably the one you will use the most in any programme. A flowchart is a good way to design the logic of a programme, and questions boxes (e.g. is user male?) will usually have two possible answers (i.e. binary): yes or no. This works well with a loop with a binary conditional:
Where there are more than one possible condition sets, the
elseif....else series can direct the run through the appropriate script.
Using switch (JS), if the number of condition matches exceeds 3, the
switch function provides an alternative to the
elseif....else will continue to work for any number of condition sets, but it begins to become a little cumbersome. If nothing else,
switch is a lot easier to read for multiple possible conditions.
switch allows for a non-match of conditions, with a
break instructs the programme to exit the
switch function after completing the code which satisfies the set conditions(s).
The order of condition statements means that the
switch function will break after the first condition to be found true. In the following example, the hour of day is assigned to variable
h, which is then tested against a sequence of conditions in a
switch construct. Since the conditions of 14, 15, 16 are tested prior to
< 18, if the time is mid-afternoon the message
"Good mid-afternoon person" will be returned instead of
"Good afternoon person". Similarly, there is no need to specify afternoon as between 12 and 18, since the morning test is made before the afternoon test.
The ternary operator (?) is a very useful shorthand for the yes/no question. e.g. If the person is male (i.e. returned by a radio button in a form), use 'Mr' salutation, otherwise 'Mrs.'.
This is superbly useful in tables, where every second row can be styled a different colour. Is current class white? If yes, make class grey, otherwise white.
Using a conditional statement, loops can run a sequence as many times as required, and break when the condition is met. Loops can be of different types:
The loop is run if the preset condition is true. While x is less than y, do z, otherwise pass on to the next part of the script.
This loop is run until the post-set condition is true. Do z, then check if x is less than y. If not, do z again, otherwise pass on to the next part of the script.
Note the changes required to the initial value of the counter, and the condition to be checked, since the counter is incremented after the first loop run.
A very common way of looping is to run the sequence for a set number of times. The difference to the
while loop is the placing of the condition in the for statement, rather than in the body of the loop. Do
z i number of times.
In handling large amounts of data of a similar type, such as from a database, it may be useful to run a loop until a value is found, and then exit the loop.
This operation could be executed by a
while loop, but the
for allows a maximum number of trials to be set ( i <= 10000), which prevents a logic error in the function sending the programme into an infinite loop, which you don't want, because it can cause all sorts of problems for servers and users.
An alternative to
continue, which interrupts the loop for that iteration only.
Content © Andrew Bone. All rights reserved. Created : June 3, 2014 Last updated :March 11, 2016
The most recent article is:
View this item in the topic:
and many more articles in the subject:
Science resources on ScienceLibrary.info. Games, puzzles, enigmas, internet resources, science fiction and fact, the weird and the wonderful things about the natural world. Have fun while learning Science with ScienceLibrary.info.
1825 - 1895
Thomas Henry Huxley, 1825 - 1895, was an English biologist, most famous for his fervent defence of Darwin's Theory of Evolution.
Relationships are a lot like algebra: have you never looked at your X and wondered Y?
Website © contentwizard.ch | Designed by Andrew Bone