What is Looping?
Looping refers to the repeated use of one or more steps.
There are two types of loops.
- Fixed Loop: Fixed loop is the loop where the operations are repeated a fixed number of times. In this case, the values of the variables inside the loop have no effect on the number of times that the looping operation is performed.
- Variable Loop: Variable loop is the loop where the operations are repeated until a specified condition is met. Here, the number of times that the loop is repeated may vary. Searching for a particular item in a list of items is an example of variable loop.
Loops are also referred to as backward jumps. These jumps may occur either after meeting a specified condition in the process or after doing a certain computation.
LOOPING – THE FOR…..NEXT STATEMENT
The FOR…NEXT statement in BASIC is such a control structure. The FOR…NEXT statement is very useful when a program requires that a set of instruction be executed repeatedly a given number of times. The syntax of FOR…NEXT is as below:
Here u is a numeric variable while e1, e2 and e3 may be numeric constant, variables, or expressions.
– u controls the looping operation and therefore it is known as the control variable of the loop.
– e1 is the initial value of u.
– e2 is the final value of u.
– e3 is the increment value assigned to u for every repetition of the loop.
– The step e3 may be positive or negative, but it should not be zero to avoid infinite looping.
– The STEP e3 is optional. If it is not present, a value of +1 is assumed for e3.
– The body of the loop consists of all statements between the FOR and the NEXT statements.
The flowchart of ‘FOR…NEXT’ statement is as below:
Counter – A numeric variable used as the loop counter.
- Start and End – The initial and final value of the counter.
- Increment/Decrement – The amount the counter is changed each time enough the loop.
FOR I = 1 TO 10
Nesting the Loops
There are several problems which involve two or more loops and many times they are contained within each other.
A loop within another loop is known as nested loop.
Jumps in FOR….NEXT Loops
A jump from one statement to another within a loop is allowed. It is also permitted to jump out of a loop but it is prohibited to jump into a loop. When a jump is made out of a loop the value of the control variable is available and can be used, if necessary. The value of the variable would be the latest value the variable had inside the loop. Whenever such a jump is made, it is always desirable to print the latest value of the control variable to know at what stage the jump was made.
While trying to use the FOR…..NEXT loop, the following Points to be Remember
1) The control variable should not be changed inside the loop.
2) The control variable can be used in the body of calculations as one of the variables of an expression.
3) When loops are nested they should use different control variable names.
4) The control variable value is not defined after the loop is completed in the normal way.
5) Do not jump into a loop.
6) A jump out of a loop is permitted and when it is done, the value of the control variable is available for use, if necessary.
7) While nesting loops, care should be taken to avoid overlaps.
8) When dealing with non integer variables in FOR statement one should be careful about the rounding off errors.
WHILE and WEND statements in pair executes a series of statements in a loop as long as given condition is true.WHILE…..WEND statement is an entry controlled loop statement. The general form is
When the WHILE statement is encountered, the condition is evaluated. If the condition is true, the statements between WHILE and WEND are executed and the control is returned to back to the WHILE statement. The condition is again tested and if it is still true, the process is repeated. If it is not true, the execution returns to the statement immediately following WEND.
This kind of loop processing is used when we don’t know how many times the loop should be evaluated.
I = 1
WHILE I < 10
I = I + 1;