Thursday, December 4, 2014

Round and round and round...

Repetition is such a mechanical thing!!!

Yes, its true. you can not go round an round and round in the same path for a whole day or may be for a month or may be for a year or may be for years.

Is it possible at all ? I can't do that, can you ?
No, I can not do that either.
Round and round and round is good for human race machines.
Think of the scenario, if wheel was not invented at all or it could not perform round and round and round, how the civilization would look like ?

Stuck, right ?


Well, this is also true in our digital world or lets call it digital ecosystem.

Wait, computers have wheels, I know but that is rather internal architecture of the PC. Are you going to demonstrate something to use them as part of our Java knowledge ?
No, I am not going to destroy your PC. I will here demonstrate how you can perform ROUND AND ROUND AND ROUND in your Java code.

CAUTION: Use these constructs very carefully, otherwise, your program will never complete its execution and you can not know about the fault before handed in the time of compilation until and unless you are executing the program on your own.

With said that we are now going to look at the different constructs that perform the repetitive jobs in Java.
These construct is also known as Looping in programming language.

We can construct different loops using while, do-while, for and for-each construct.
Let's take a look on each of them,

while:
while loop has the form as follows,

while(<boolean condition>){
 //Repetitive tasks to perform
}

In this case, looping starts with checking the boolean condition, if the condition is true then looping starts and continues to execute until the condition gets false.

do-while:
do-while loop has the form as follows,

do{
 //Repetitive tasks to perform
}while(<boolean condition>);

In this case, the boolean condition is not checked at first. Rather it executes the loop once and then checks the boolean condition. If the boolean condition is true, it continues to loop otherwise, it breaks the loop.

for:
for loop has the following form as follows,

for(<initialization, boolean condition, update>){
  //Repetitive tasks to perform
}

In this case, you have to initiate the loop with loop control variables and then it checks for the boolean condition and loops till the condition is false. On each iteration the initialized value of loop control variable gets updated.

Let's put all in code...



 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  *   
  */  
 public class LoopTest {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int i = 0;  
           while (i < 10) {  
                System.out.println("Iteration in while loop, value of i: " + i);  
                i++;  
           }  
           do {  
                System.out.println("Iteration in do-while loop, value of i: " + i);  
                i++;  
           } while (i < 10);  
           for (i = 0; i < 10; i++) {  
                System.out.println("Iteration in for loop, value of i: " + i);  
           }  
      }  
 }  

What is i++ ?
Well, it is short hand for increment and assignment.
This simply means, i = i + 1;

We'll check this one and some another construct of short hand assignment later. Let's concentrate on loop for now.

Well the output looks like the following,
 Iteration in while loop, value of i: 0  
 Iteration in while loop, value of i: 1  
 Iteration in while loop, value of i: 2  
 Iteration in while loop, value of i: 3  
 Iteration in while loop, value of i: 4  
 Iteration in while loop, value of i: 5  
 Iteration in while loop, value of i: 6  
 Iteration in while loop, value of i: 7  
 Iteration in while loop, value of i: 8  
 Iteration in while loop, value of i: 9  
 Iteration in do-while loop, value of i: 10  
 Iteration in for loop, value of i: 0  
 Iteration in for loop, value of i: 1  
 Iteration in for loop, value of i: 2  
 Iteration in for loop, value of i: 3  
 Iteration in for loop, value of i: 4  
 Iteration in for loop, value of i: 5  
 Iteration in for loop, value of i: 6  
 Iteration in for loop, value of i: 7  
 Iteration in for loop, value of i: 8  
 Iteration in for loop, value of i: 9  

The output looks cool, right ?

  • We initialized i to 0 
  • We incremented in each iteration of while loop until it gets to 10
  • In each iteration it printed the value of i.
  • In each iteration we updated the value of i
Next we worked with do-while
We have used the value of i as it is, no change in the value.

BUT what is this ? Why do-while is printing the value of i. It should not print cause i is already 10.
That's the catch, do-while will always perform the steps in the loop body at least once before it even checks the boolean condition.
Be aware of the fact while on real life works. Things can be messed up due to this only.

Next, we again used the same variable i and initialized it to 0 and checked for the condition whether i is less than 10 or not.

Again question, where are we updating the value of i ?
We don't have to update the value manually it will be done in the loop construct itself, we just have to define the update rule. Check for the last statement where it is i++.
Once the loop body is executed, it updates the loop control variable in each iteration used for the loop.

Well, that's all when things are settled perfectly fine using loops. But it can certainly mess everything due to a very simple typo.

Check the following program,
 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  *   
  */  
 public class LoopTest {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int i = 1;  
           while (i > 0) {  
                System.out.println("Iteration in while loop, value of i: " + i);  
                i++;  
           }  
      }  
 }  

Output is deliberately left as an reader's exercise. If you are stuck, just check the while loop construct, you will get answer where and how to fix. If still you are not sure, re-read the post again. If still you are unable to find the issue, comments are welcome...


Well, that's for now.
Wait, you told something about for-each. What about that ?
Well, I will go through them as and when time comes. Till then enjoy looping.


Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment