Difference makes the DIFFERENCE ## LOOPS OR ITERATIONS

### REPEATS THE SAME TASK FOR SAID TIMES

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.  ### Components of Loop:

Every Finate Loop should ideally have an Initial Value, Counter/Increment Variable and End Value.

• Initial value is where the programme starts working/counting, or the variable in which the initial value of the loop is stored.
• Counter varaiable starts counting the number of iterations,
• Increment variable steps up the loop,
• The default incremental value of any loop is 1 (one). For example, to count all odd numbers, start the loop with 1 (set the initial value to be 1) and increment the value by 2
• End Value stops the iteration.  ## Infinate Loops:

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.

## Finate Loops

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.

## Definite Loops

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

for example:
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. ## InDefinite Loops

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

• This means, the lines of code after EXIT statement and NEXT statement are not executed.
• These EXIT LOOP statements are generally used with IF Condition

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. ## to Summarise:

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

## Examples for Practice:

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.

1. Print a series of 10 odd numbers
2. Print a series of 10 multiples of 5
3. Count the number of vowels in a given name.
4. Print all the consonants in a given name.
5. Print a series of 50 numbers, check, if each number a multiple of 3 or 5 or both.
6. Write a program to print factorial of any given number.
7. Print a series of given 20 numbers, check if the number is odd or even number, and write beside the number, if it is odd or even eg: 1 - Odd, 2 - Even, 3 - Odd and so on...
8. Print a series of 20 prime numbers, print beside the number, if it is prime number or not eg: 1 - prime number, 2 - prime number, 3 - prime number, 4 - not prime number and on...
9. Print all the alphabets and Check if the given alphabet is a vowel or consonant and print accordingly eg: a - vowel, b - consonant and so on...
10. Print a series of 100 numbers and check if the number falls within any of the given 4 buckets eg: 0-25, 25-50, 50-75, 75-100, print accordingly. Check if the number 25 falls within first bucket or in second bucket. Now re-write the same programe to get the output in 0-25, 26-50, 51-75, 76-100.
11. Write a program to print the numbers in reverse order, eg: 10, 9, 8, ...
12. Write a program to print, every alternate alphabet in a give name
13. Find out the number of leap years from 1900 to 2000 (inclusive).
14. Write a progrm to print the squares of the series from 1 to 10 eg:1, 4, 9, 16,...
15. Write a program to print the square roots of a series from 1 to 10.
16. Write a program that could generate only prime numbers, make us of polynomial expression. Hint: Babbage Function
17. Write a program to generate random numbers and print the numbers and their sum
18. Write a program to print Fibonacci Series (eg: series - 0, 1, 2, 3, 5, 8, 13, 21, 34...)
19. Check if a given word is pallendrome or not eg: MADAM, where same number of alphabets are repeated on either side of the central letter

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.