DO FOR WHILE LOOPS!!!
By: Nonso Biose
Tags: Java, JavaScript, Programming, Loop, Software Development

Image gotten from Google

Hey and welcome back to my second post on Java this year. I know I have not been faithful to my dedicated fellow blog readers who have been keeping up to date with my regular post. My bad and I hope to make up for it.

Now all said and not yet done, lets quickly dive in deep to today’s post which is interesting and very very useful in Java(“Every concept in java is useful”). Today’s talk will be on the concept of LOOPS.

Now what is a loop, types of loops, why are they needed and when do I need to use it….. these are the concepts to be dealt with today.

Simply put, a loop which is used in all kinds of programming languages including Java is just an automation of a task. This means that it is a concept in java that carries out an automation of any kind of work. Going further, a loop can also be described as a tool in Java that is used to carry out a task repeatedly without having to do explicitly repeat yourself anytime the task is needed.

In Java, there are three types of loops which do the same thing i.e automate a task with slight differences. The first loop in java is known as a FOR LOOP. The for loop is defined as the following:

Now the above picture is a graphical explanation of the FOR LOOP which is the most widely used loop in Java. So you would, as I have said earlier, use this loop when you want to automate any kind of task in Java. The task could range from printing something to screen to adding an entry to an Array(“or any other form of storage”), up to retrieve the data and so on. Just anything can be used with the FOR LOOP.

Looking at the above pic, I am trying to further explain the FOR LOOP concept as is the tradition here on my blog.

I believe that the pic above is self-explanatory (“it is good for a software developer to think smart”). With a side by side comparison of the two above posted pics, you can see that the first section in the parenthesis of the FOR LOOP is called the initializer.

INITILIAZER: This is used to start the automation of the task to be carried out by the FOR LOOP. So we store any value in our variable “ i” (“the variable can be any letter or word, but its best to keep it simple”). The value of the variable can be any primitive data type except for “Boolean” (“still a best practice to use integers”).

TEST CONDITION: Now after initializing our desired variable with a value, to keep the loop running we need to create a condition that must resolve to true. Else the loop will break(“ in other words you need a condition that will be true for as long as you want your loop to do something else it will stop looping”).

INCREMENT / DECREMENT: This last part in the parenthesis is used to either increase the value of the variable or decrease in other for the loop to keep running i.e for the test condition to keep evaluating to true. Basically if you look at the first uploaded pic, our loop will do something once at i = 0 and then it will be increased by one and still do something because the condition is true until it gets to 5 and then the loop breaks out(“ i=5 cant be less than 5”)……(“decrement can also be used here but with a reverse of the conditions equality sign”) .

Finally, as I have said earlier, any form of code logic can go into the curly braces.

Moving on to the second loop in Java, I introduce to you the WHILE LOOP. This as said earlier does the same thing with the FOR LOOP but with a slight difference which is the fact that when you use a FOR LOOP, you know exactly when you want the loop to break out. but in a while loop, you don’t.

I am sure that the pic is explainable, so while the condition is true, something will be done and then the value of the variable is increased or decreased. The next loop is known as a DO-WHILE LOOP. This loop is not widely used in Java programming but still serves some certain important purposes.

Looking at the above loop, its kinda like a reverse of the WHILE LOOP, what this does is to do something before testing to see if the condition evaluates to true. There would be times in your software development career when you will need a loop like this, but as of now, I have never had any course to use it. Thinking of examples, we can say that a loop like this can be used when….

we want to create an app that needs to access a database,so on loading the app, a connection is established to the database and then the condition to be tested may be the fact that as long as the users screen is on, stay on to the database for easy storage and retrieving of data.(“this would help speed up data transfer in the app as the users screen may shut down and would require connection when data is required”).

In addition to the loops explained above, lemme extend one to another version. The FOR LOOP has another version that is the shorthand version of the traditional for-loop.

The above loop is called an ENHANCED FOR LOOP or a FOR-EACH LOOP. it is a cleaner and shorter way to construct a for loop in Java. It does the same thing as the FOR LOOP but is only used when you are looping through a list of things and intend to carry out actions pertaining to that object you are looping through e.g. an Array, a Map, Linked list etc. What happens in the parenthesis is that the items at each index in the array (“collections”)gets stored in the variable I (“this variable can be declared to be anything)and can be used at any particular index for anything.

Note: you can use the first type of for loop to create objects and add them to an arraylist or you can use it to print random numbers but an enhanced for loop can’t do that, it is streamlined in it its abilities. Note: the type of items you iterate through in this kinda loop, affects the data type declared for your variable. This means that if looping through items of a datatype of String, the datatype of the “i” variable must match it.

Finally, I have been able to explain to you and also build upon previous knowledge the concept of loops in JAVA. For the rest issues to be discussed in this topic as noted earlier, go to the next line else goto the end.

ME: Now you know about loops, cool right ??? YOU: ya! … but why is it needed?? ME: when ever you want to automate an action like copying out list of names from an array of names(“its not best practice to do it manually”). YOU: Cool, thanks.

FINAL words to you my dear reader, Please take time out to DO FOR WHILE LOOPS(“Guess you get the pun here”) and solidify your Java knowledge.

Am off to my usual’s …..</coding>.