Search This Blog

Thursday, January 31, 2013

Understanding Java - while Loop



While Loop

While loop executes a code block while the Boolean expression remains true. 

Syntax:   while ( booleanExpression ) {
                          code block
                  }

Points to Ponder :
  1. The Variable used in the Boolean expression must be initialized before the while statement.
  2. There must be a valid condition test to stop, otherwise, it becomes an infinite loop.
  3. There must be a statement in the loop to modify the variable that controls the condition.


'n' is a local variable and hence needs explicit initialization. Java compiler flags an error.




'n' needs to be initialized. Here is the second code( Example of Infinite Loop )


















Due to no change in the value of the variable 'n',  it keeps printing 1.


Proper While Loop Example :




A boolean value can be used instead of a boolean expression. If true is used in a while loop, it behaves as an infinite loop. To stop this loop, the break statement can be used.



Now here is something very special about the nature of while


Java compiler does not allow the use of false literal in the condition/boolean expression. A smart compiler :). It flags a compile-time error "unreachable code".  But here is something more wonderful about the Java compiler.


Variable with value false is allowed... :)

Thursday, May 3, 2012

Attitude Towards Technology

It feels strange, in a world where people don't have time even for other human beings, the attitude towards technology is a hard-to-digest term. Lack of time is a great blessing, as the internet is full of codes and other related information. One can always download, edit, and personalize the code and use it. To be at the top in the shortest span (like shortest path J) is the driving force that leads one not to have a learning attitude.

Learning, sometimes, rather than most of the time, is a challenge to the existing mindset. We assume that there is an age for learning. Once one is familiar and comfortable with one technology, one approaches the other technology with the same mindset. That is a problematic approach, as every technology is created as a solution to a certain problem set. As a trainer, I face many such questions. When one decides to learn a particular technology, the groundwork is required. Learning attitude needs to be different.

I already know is one of the most common barriers in the learning process. One may have learn the subject in college or worked on it for some time. Sitting and listening is boring and a sheer waste of time, and reception is nullified. Rather than learning, the mind is working on weaving questions that may be prejudiced. It may lead one to become inflexible and rigid rather than responsive and thoughtful. To have a good relationship with technology helps being well-versed and writing magical codes, which may do wonders (optimized and performance-oriented).

Having patience to listen and being thoughtful in vertical and horizontal directions can help one master the technology. Learning the technology to its core, what it provides, why it provides, how the code works, and where it can be applied brings the joy of knowing.  The right amount of curiosity with good analytical tendencies acts as a catalyst in the learning process. 

Do you think learning differently will change your questions? Do you think changing a question can change the way one will code?

Learning attitude is an art that helps not only in technology but also in handling one’s life.

By Nancy K A N
10/25/2010