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.

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.

OutPut -

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:

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.

01. Initialization

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.

No comments:

Post a Comment

Author Details

I am Prateek Singh and I have been coding and learning how to program, for a while. I have been learning Java and other programming languages by myself and I thought it would be great to share with the people who are interested in Self Learning. I will be sharing what I am learning and it would be great to learn and share together.

Author Description

Programmics is an online resource where you will be finding programming tips & tricks to help you out when you get stuck at any point.

Recent

Comments

Categories

About Me

Hi, I am Prateek Singh and I am a self learned programmer & a web developer. I have been self learning programming and web developement since long. Here, at Programmics, I will be sharing all the secerets of self learning.Learn More →

About Us

Programmics is an online resource where you will be finding programming tips & tricks to help you out when you get stuck at any point.