A loop is used in a program to change its flow by executing an instruction or group of instructions several times as directed by the programmer. This saves programmer’s time because if  loops were not available he would have to write those instructions as many times as the loop iterated.

Java Loops:

Java has 4 basic kind of loops
1. For loops
2. While loops
3. Do-while loops
4. For each loops
Here we will only discuss the first 3 as they are most commonly used in every day programming.

For loops:

For loop is the most commonly type of loop used  in all programming languages including  Java.  For loop has three parts which are:
1. Initialization: An integer in initialized
2. Condition: Its a condition on the initialized integer as long as this condition is TRUE loop iterates and continues but when this condition gets FALSE the loops breaks
3. Increment: The value of the loop increments by an amount defined in the loop each time the loop iterates or the condition given is TRUE

Syntax:

Suppose we are given a task to print “Hello” 10 times, now there are two ways
1. Write System.out.println(“Hello”) 10 times which would be hectic
OR
2. Use a for loop to do this job as shown below
Both of the methods will give same output but we can easily see that using for loop has saved us time which is every thing now-a-days. Anyway for loops can be nested which means there can be one for loop in another such as
for(int j=0;j<10;j++){//loop 2
System.out.println(“Hello”);
}
}
In this example we used a nested loop(loop within a loop) which would print “Hello” 100 times as 10×10=100, now reader would ask why would it print “Hello” 100 times?. Well its because each single time loop 1 iterates loop 2 completes its all(10) iterations. So, as loop 1 eventually completes its 10 iterations the total times loop 2 was iterated is 100 as for each single iteration of loop 1 loop 2 iterated 10 times.
To execute an instruction infinite times(such instructions can be used in hardware projects or interrupts) we can use an infinite for loop
We can use for loop to access an array or initialize an array.

Example 1:

Example 2:

Now suppose we are given an assignment to find all 5′s in an array of 100 elements. In this case we don’t know beforehand that how many 5′s are in our array and what if we were asked to get their index or location in this array also. We would do something like this

While & Do-while loops:

While loop:

While loop is very simple it has to check a condition which if  TRUE the loop iterates else if FALSE the loop breaks.

Syntax:

Example:

Suppose once again we need to display “Hello” 10 times but this time with while loop this can be done as shown below
The condition x<11 will be FALSE when value of x will be 11 and that will happen after the statement System.out.println(“Hello”); is executed 10 times. So “Hello” will be printed 10 times.

D0-while Loop:

A do-while loop is same as while loop except that it executes the instruction(s) written in ‘do’ braces without checking the condition of  ’while’  for first time only.

Syntax:

Example 1:

count++;
} while (count<11);Will print “Hello” 10 times.

Example 2:

We can see that in this example even though condition count<11 is FALSE because count is initialized as 100 but “Hello” will be printed once as for the first time a do-while loop does not check condition and executes the statement(s) placed between braces of ‘do’. But before next iteration loop will break as this time condition will be checked.


0 comments :

Post a Comment

 
Top