Iteration through Loop in Java with Examples! - Programmics! - We Love to Code.
demo image

Iteration through Loop in Java with Examples!

Share This
Any beginner with Java deals with different topics, concepts, and ideas while starting out. And one of those goes like ... what are the different types of loops in java and how to use them?


So, here... we're about to discuss the types of loop in java with examples included.

Kicking it in the first gear, Loops are repetitive executive code blocks that execute a command or an output again and again until the test condition or the iteration turns false.


Loops and Iteration


Fooling around Hello World!

Suppose you want to print "Hello World!" once in the output, you can easily do so by writing out some basic lines of code:

import java.io.*;
class HelloWorld
{
public static void main(String []args)
{
System.out.println(" Hello World! ");
}
}

but how printing the same word 10 times? Now what we gonna do?

Well, we've two choices here one tough and the other one stupid simple.



First things first, how about printing " Hello World " for 10 times manually through the program without the use of loops?

import java.io.*;
class HelloWorld
{
public static void main(String []args)
{
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
System.out.println(" Hello World! ");
}
}



OutPut -

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!


Now that's tough, right? But stupid. Doing it for 10 times won't take any much extra mile of the effort down the way but how about printing or executing a statement for 100 times, or 500 times, or 1000 times, or maybe 10,000 times?

Is anyone really going to type in 10,000 lines of code for getting this job done? Naah!! :(


It would be another level of foolishness and hell lot of work for just a little task, and thankfully we have loops for that.


03 Loops in Java

Java uses 03 different types of loops and iteration statements to get the heavy load of a similar kind of job done and thus saves the coder's life!

Now each loop has its own functionality, behavior, syntax and working process which are applied in different algorithms to get different jobs done.

Some loops have iteration condition to go through the code blocks and deal with commands to represent the desires output while other loops are designed to serve a true-true-false statement, which means the commands, output or statements will be executed or printed as per the statement turns true and the loop will exit once the statement turns false.

And now we are going to discuss these 03 different kinds of loops, their structure, syntax, use, and behavior.



Types of Loops in Java

Loops are mainly of two types -

Entry Controlled Loops

These are the loops in which the test condition is checked before the iteration of the loop.
For Example - for() loop & while() loop.

Exit Controlled Loops.

These are the loops in which the test condition is evaluated after each iteration is completed, that means in this type of loop even if the test condition goes false for the very first time, the output will still be printed out once.

For Example - do-while() loop


01. For() Loop

It is one of the most used loop condition of all time, due to its simplicity, iteration, and flexible algorithm.

This loop is based upon an iteration statement that continuously compares the iteration variable with the test condition and accordingly executes the code blocks followed by the 'for() Loop'.

This is the syntax of for() loop, where we've initialization variable, test condition, and iteration.


Now let's understand what these terms denote and how they work in the for() loop.

for loop


01. Initialization Variable

When the for() loop starts, the initialization portion of the loop is executed that initializes the variable with some definite or indefinite value that set's the starting limit of for() loop.
For Example -
for(i=0; i<=4; i++)

Here, i=0; is an initialization of the variable 'i', which means the for() loop will start from 0.

02. Test Condition

This is a comparative statement that compares the value of the initialized variable to a fixed value to a variable and then depending upon the result the loop breaks or executes.

In the above example, 'i<=4' is a test condition if this condition goes right then only the blocks of for() loop will be executed, otherwise, the loop will break.


03. Iteration

Once the test condition is satisfied and the loop is executed, the iteration changes and then the whole process repeats again. The initialized variable is increased or decreased according to the iteration and then is again compared with the test condition for the loop execution.

In the above example, 'i++' is an iteration of the for() loop that will change the value of the variable 'i' every time the loop will be executed.

Print "Hello World!" 10 times with For() loop.

Now let's again try to print, "Hello World!" 10 times but now with the help of for() loop.

Code -


import java.io.*;
class HelloWorld
{
public static void main(String []args)
{
int i; //declaring a variable for iteration
for(i=1;i<=10;i++) //running the loop 10 times
{
System.out.println("Hello World!");
//anything inside the braces of the loop gets executed.
}
}
}


OutPut -

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

See? It did the same job, just the code length got shortened. It also reduces the computing load on the system and also uses the less amount of memory than that of the earlier algorithm.

NOTE: The shorter and precise an algorithm, the better. An algorithm is effective if it uses the less amount of the computing power and the memory space of the system to get the job done.


02. while() loop

This is also an entry controlled loop, where the condition is checked first and then the loop is executed, just like the for() loop.

It has a very simple syntax and algorithm. It repeats the statement under its body just as long as the conditions fall true.

This loop also has three parts:


while loop

import java.io.*;
class HelloWorld
{
public static void main(String []args)
{
int i=1; // initial condition
while(i<=10) // test condition
{
System.out.println("Hello World!"); //printing the output
i++; // iteration
}
}
}


01. Initial Condition

Just like in the for() loop. It also has an initialized variable, 'i' by which the condition will be checked.

In the above example, 'int i=1;' is the initial condition.

02. Test Condition

This expression checks for whether or not the initialized variable passes the test condition. If yes, then the statements under the loop's body will be executed else the loop will break.
In the above example, while(i<=10) is the test condition.

03. Iteration

After successfully executing the loop, the initial variable goes through iteration for further loop execution.
In the above example, 'i++' is the iteration for variable 'i' after the loop successfully executes once.


04. The output of the while() loop - Hello World! algorithm.

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!


03. Do-While() Loop

Unlike the other two loops in java, do-while() loop is an exit controlled loop which means the loop will execute once and then the test condition will be checked.

And in case the test condition turns false, the loop will terminate after being executed once before the test condition check.

do-while loop

Just like the other two loops this loop also has three parts:

import java.io.*;
class HelloWorld
{
public static void main(String []args)
{
int i=1;//initialization
do
{
System.out.println("Hello World!");
i++;//iteration
}
while(i==0);//condition check
}
}

01. Initialization

Initializing the test variable 'i' for loop condition check.
In the above code, int i=1; is the initialization step.

02. Iteration

Iteration is provided in the body of the loop which will be effective for once even if the test condition turns false.
In the above code snippet, 'i++' is the iteration for the initializing variable 'i' which will iterate its value.


03. Condition Check

Checking the initialized variable 'i' comparison with the test condition for the execution of the loop.

In the above code snippet, 'while(i==0)' is the loop test condition for further loop execution.



04. The output of the do-while() loop - Hello World! algorithm.

Hello World!

This is only printed once because do-while is an exit controlled loop and the test condition has turned false. And the exit controlled loops are executed at least once before checking the condition statement.


Geeky Gene's Thanks Giving!


Thank you for landing on Programmics and reading Geeky Gene's contribution post. Have a nice day! :)

No comments:

Post a Comment