Tutorials : Making the Switch to Java :

Graeme Kerry is a first year computer systems engineering student from the UK, studying at Lancaster University. He has been programming in Java for about 3 years. Outside of studies he enjoys sports and socialising with friends. Kezz's Java Tutorials are written in Graeme's spare time.

If Statements

Welcome to the 4th installment of my Java programming tutorials. In this tutorial I will discuss if statements and how they can be used to implement decision making in your programs.

First of all lets have a look at an example of an if statement. This will show you the syntax in Java.

if (age >= 18)
{
System.out.println("You are an adult");
}
else
{
System.out.println("You are a minor");
}

Just by looking at the code listed above it should be pretty obvious as to what the code does. It checks the value of the variable called age, if this is greater than or equal to 18 it prints "You are an adult", and if it is less than 18 "You are a minor" will be printed.

This shows the general structure of an if-else statement in java:

if (condition)
{ //DO THIS }
else
{ //DO THIS }

A useful feature of if statements in java is that you can nest them together to form complex decision making statements. The syntax of this is listed below.

if (condition) { //DO THIS }
else if (another condition) { //DO THIS }
else if (yet another condition) { //DO THIS }
else { //FINALLY IF NONE OF THE ABOVE CONDITIONS WERE MET DO THIS }

Finally for this tutorial I am going to go through some special ways of using if statements and conditions. They are explained below

if (!condition)

This statement means if the condition is NOT true. eg if (!(age==18)) means if the value of age is not 18

if ((condition1) && (condition2))

This statement means if condition 1 AND condition 2 are true. For example if ((if age>=18) && (gender.equals("male"))) translates to if age is greater than or equal to 18 and the value of the string gender is male The .equals(String) method is used for testing if 2 strings are equal, all other variable types use ==.

if ((condition1) || (condition2)

This statement is very similar to the above one except that the || means OR. So the statement translates to if condition1 OR condition2 are met

That's all folks....

I'm afraid there was a lot to take in in this tutorial (in very short space), however you know what they say practice makes perfect. Thats why I have a small exercise for you:
You should now be able to write a program that prompts the user for their name, age and gender and return it back to them with additional information (eg whether its legal for them to drive yet or what year they were born in). You should only prompt the user for those 3 pieces of information and try and return as much info as you can from the details given.
If you have any trouble writing this or want to comment on the tutorials email me

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.