Difference makes the DIFFERENCE
Categorised Menu List
Loops are one of the primary structures without which almost any kind of iterative programme can be written. These structures primarily help, in doing the same job again and again for a given number of times or until the said condition is satisfied (either TRUE or FALSE). For example: to get numbers from 1 to 100, ideally we have to write a statement like print 1, print 2, print 3... print 100 (a hundred number of times) where as with a loop, the program, runs for 100 times, to print the same statement.
All the given looping structures may not exist in all programming languages, but certainly LOOPS will. Almost any programming language will support, atleast one Definite structure, atleast one pre-checking looping structure and atleast one post-checking structure . I hope these are self explanatory, but still for the benefit of beginner, I will try to explain to some extent.
Every Finate Loop should ideally have an Initial Value, Counter/Increment Variable and End Value.
These Loops, certainly, will have a starting point, and hence they start at a certain value but, because they donot have a stop value, the loop goes on. They never stop at any point of time, no matter, the how many number of times the loop may be executed.
Hence appropriately named as Infinate Loops. These loops are not generally used in any practical situation. So, this type of loops are considered as logic errors
Though a loop may iterate a million times, but still that million is the end point and hence cannot be considered as infinite loop. Which ideally means, a loop goes on iterating until the user stops intermittently.
These loops does a certain number of iterations (repeats the same code). We cannot tell as to how many iterations does it iterate., but certainly, it starts at one point and ends at another point. Some times, the start and stop values may be the same.
These kind of loops have a definite start value and a definite stop value. Considering the case of FOR loop, it has a definite start value and a stop value. If there be any need to stop in between, we use EXIT FOR statement to close the loop or exit the loop
in a FOR loop, start value is 1 (one) increment value is one (it is by default taken as 1) and the end value is 10, in second example, start value is 1, increment value is 2, end value is 10. Therefore, this loop is called DEFINITE loop, hence it is obviously a FINATE loop.
These kind of loops have a definite start value but indefinite stop value. Considering the case of FOR loop, it has a definite start value and a stop value. If there be any need to stop in between, we use EXIT statement to close the loop or exit the loop Considering the above example, with the following structure -
examples 3 and 4 for Post-Checking and examples 5 and 6 for Pre-Checking
Note: in examples 3 and 4, the loop (code between DO and LOOP) does run atleast once before it come to validate initial value
Note: in examples 5 and 6, the loop (code between DO and LOOP) doesnot runs atleast once before it comes to validate initial value
To stop execution of loop in the middle of the programme, or to break the looop, after a certain condition is met, Programming construct provides with Exit For for For Loop, Exit for other loops, when the program executor (either compiler or interpretor) find this statement, the loop stops working and moves to NEXT in case of FOR Loop and End Do in case of Do..While loop
For Example, if a program is searching for records in a database, where a database has more than 1000 records, if the required record is found at, say 45th position, it is unnecessary, for the program to complete 1000 iteration, which is waste of time and system resources.
We need to consider an example, as to how, we can apply this functionality.
Consider a database, where you have 'n' number records where the value of n may be 0 (i.e., no records) or 100 records or 1000 records, so, the value of n varies on the number of records a database has.
Now, if I make use of FOR loop where it starts with 1 (i.e., the first record) and ends with 100, the loop may fail when the database has more than 100 records. This is because, by definition: a FOR loop, should have a defined start and stop values, whereas the rest are not so.
to put it in one line: with a START value and a STOP value, FOR Loop is the best, in all other situations, DO WHILE or DO UNTIL can be used
The following examples can be used in programming... Please practice all the examples with all types of loops available eg: For Loop, Do..While Loop, Do..Until Loop etc... (one Definite Loop and Two pre-checking types and two post-checking types). Observe the difference, while they are being executed and in output.
I will try to collect more examples for practice, because, strength of logic is directly proportional to the number of different kinds of examples, one works with.