Writing Java Code - Methods
Written by Alex Armstrong   
Thursday, 20 January 2011
Article Index
Writing Java Code - Methods
The flow of control
Conditionals - if
While
Try it out

The Demo project

Start a new Java project and add a JFrame as described in Chapter 1. If you don't know how to do this go back and look at the example program in Chapter 1.

Now switch to the Designer and place a button on the form. Double click the button an you will be transferred to the Code editor ready to type instructions into the button's click event handler. What we are going to do is enter a single example of each control structure into the button's event handler so that the commands are executed when you click the button.

All of the code listed below is to be entered into the button's click event handler.

If

First we look at a simple if statement:

int total=100;
if(total==100){
 System.out.println(total);
}

The command println stands for "print line" and it will print the value of the integer variable total in the Output window which you can see in NetBeans' default layout just below the source listing.

If you type in and run the above what do you expect to see?

Answer: you should see 100 printed in the Output window. Now change total to be 99. Now you should see nothing at all printed.

If Else

An if then else is fairly easy:

if(total==100){
System.out.println("value is 100");
}else{
System.out.println("value is not 100");
}

When you run this you will see "value is 100" printed.

What do you see if you change the value of total to 99?

You will see just the message "value is not 100". Notice you only ever see one of the two messages.

For

In the case of the for loop:

for(int i=1;i<=10;i++){
System.out.println(i);
}

what do you expect to see printed? Be exact.

The answer is 1 to 10 because each time thought the loop i increases by 1 until it reaches 11 when the loop ends without executing the body of the loop again i.e. with the value of i set to 11.

As an aside there is a long history of using i,j and k as counters in loops - the reason is that i is the first letter of integer and a counter or index in a for loop is always an integer.

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.

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 generalises 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 in side a conditional inside a loop etc. The most 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 future chapters.

Modern Java Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

If you would like the code for this project then register and click on CodeBin.

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

blog comments powered by Disqus

<ASIN:0072263849>

<ASIN:0596009208>

<ASIN:0132354799>

<ASIN:1430234164>

<ASIN:0596009070>



Last Updated ( Wednesday, 07 November 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.