Modern Java - Writing Code
Written by Mike James   
Article Index
Modern Java - Writing Code
Expressions
Conditionals
If Else
While
Project

While

A good simple example of a while loop is perhaps the most difficult to find. One example is to just write a loop that counts just like a for loop.

For example, the following is the exact equivalent of the for loop example i.e. it prints 1 to 10:

int j=1;
while(j<=10){
 System.out.println(j);
 j++;
}

This is the reason the for loop isn't really needed. Anything you can do with a for loop you can do with a while loop - but the for loop is usually easier.

Consider now a real situation where only the while loop will do - even if it is contrived.

How many times can a number be divided by two before it is less than or equal to one?

We can simulate this by writing a while loop that divides the given number and tests for it being greater than one:

int number=10;
while(number>1){
 System.out.println(number);
 number=number/2;
}

This is should be easy enough for you to follow though but notice that the statement number=number/2 can only be understood if you don't read the equals sign as a statement that the two sides are equal.

The statement says take the contents of number, divide it by 2 and store the result back in the variable number. If you run the program you will see it print 5 and 2 as after the 2 is printed the value is divided by 2 again with the result 1 and the loop ends.

You can do the same job with a do-while loop:

number=10;
do{
 System.out.println(number);
 number=number/2;
}while(number>1);

The difference is that now the test is done at the end of the loop.

It is arguable that in this case the test being at the end makes it easier to see why and when the loop comes to an end. However the real difference between the two is that is you set number to one and run each loop the while loop prints nothing at all but the do loop prints one - its all a matter of where the test to end the loop is performed.

Also notice the way indents are used to show where blocks of code start and end. The content of an if, else, for, while and do while should be indented.

If you use NetBeans then you will find a handy auto format command. Simply right click anywhere in the code and select Format from the menu that appears.

And finally - nesting

This is a lightening overview of the basic ways you can build program in Java - or any programming language for that matter.

This part of learning to program generalizes to other languages. Once you have the basics of the default flow of control, conditionals and loops you can start to work on putting them together like basic building bricks used to create more complex things.

In general there are only two ways to put these building bricks together. You can put one after the other and then you have a conditional followed by a loop followed by a conditional.

A more complicated way of combining them is to put one structure inside another - nesting them. So you can have a loop inside a conditional inside a loop etc.

For example:

Suppose you want to print a message to say a number is odd or even. Then you might use something like 

int i;
for (i = 1; i <= 10; i++) {
 if (i / 2 * 2 == i) {
   System.out.println("even");
 } else {
   System.out.println("odd");
};

You can see that there is a for loop and within it is an if statement. The if statement is nested within the for loop and so repeated ten times. The only "tricky" part of the program is condition within the if statement. If you take an integer like 3 and divide it by 2 the result isn't a fraction but the exact number of times 2 goes into 3 i.e. 1. This is integer division. When you multiply this by 2 you get 2 which is not equal to 3 and the number is odd. If you repeat the procedure using an even number like 4 you get 4/2 is 2 and 2*2 is 4 which is equal to 4 and so the program print even. Integer division may not be accurate but it is often very useful in programming. In general if a number x is even then x/2*2 is x but if it is odd then it is smaller than x - try it.

 

The more control statements you nest the more complicated the program becomes and this is a bad thing. Over the years we have found ways of avoiding building complicated structures like this  - but this is moving on to future topics. For now simply make sure you understand the conditional and the loop; you will see both used a lot in following chapters.

 

Modern Java
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962544>

<ASIN:1871962552>

<ASIN:1871962536>

<ASIN:0596009070>

<ASIN:0132354764>

<ASIN:1849511764>

<ASIN:0321356683>