next up previous
Next: Arrays Up: The Frightened Freshers Guide Previous: Methods

if Statements

So, now you can create methods and do things with variables. Thats all well and good, but what if you want to check for some conditions? In Java, you'll just use an if statement. These take the form:


\begin{lstlisting}
if(someCondition)
{
//Do stuff if the condition was true
}
else
{
//Do other stuff if the condition was false
}
\end{lstlisting}

Now, this may look a little scary right now, but its far simpler than it looks. Below is an example of a method that restricts the input of the user so that they cannot enter a number less than 0.


\begin{lstlisting}
public void youCantEnterANegativeNumber(int newNumber)
{
...
...lse
{
System.out.println(''Number entered okay!'');
}
}
\end{lstlisting}

Aieee new things! The new things you see before you are the condition in the if statement (newNumber < 0) and the System.out.println("stuff");. The first thing is the condition for the if statement. If the condition evauates to true then the first bit (Before the else) is executed. If it evauates to false then the else section is executed. Note that never will both the if and else be executed! The other new thing is the System.out.println("Stuff");. What this does is actually pretty easy, it prints out to the java console. If you entered this code into a new Class in BlueJ then you'd see it printing to console.

There are a few little oddities about if statements, firstly you can ommit the and when you only have one statement in your if. Also, you do not necessarily need an else statement if you do not want something to happen if your condition is not true. An example of a method like this is:


\begin{lstlisting}
public void itItNothing(int someNumber)
{
if(someNumber == 0)
System.out.println(''Hello World!'');
}
\end{lstlisting}

So, this method will print out "Hello World!" if the number you feed it is 0. But wait just a second, theres something new here! There is a HUGE difference between = and ==. And thats the topic of the next section.

So whats with the = and ==? Well, you saw at the start of this document that = was used to assign a value to something.


\begin{lstlisting}
someString = ''Hello'';
\end{lstlisting}

...but also that you can use the double equals to compare things:


\begin{lstlisting}
if(someString == ''Hello'')
\end{lstlisting}

Note that these two are very, very different. So never ever have a single equals in your if statements, and never ever have two equals signs in your variable assignment. Just dont do it. Its bad. The difference between the two is that one = sign will simply set the variable on the left hand side to the value on the right hand side. Double = signs means that java will do a comparison of the thing on the left hand side and the thing on the right hand side (And so if they are the same, true, otherwise false).

Some more interesting things to note about if statements in java including using multiple conditions. To do this, you just need to join up the different conditions with two & symbols:


\begin{lstlisting}
if((someThing=1) && (someThingElse=2))
{
// do stuff
}
\end{lstlisting}

You may have noticed that I added lots of brackets. If in doubt, add brackets around things! Anyways, to show how this works, heres a nice little example thing.

If someThing was 1 and someThingElse was 2, then you would have an if statement that (effectively) looked like this:
\begin{lstlisting}
if((true) && (true))
\end{lstlisting}

Now, the double & symbol means that the whole thing will only be true if and only if (you may see this written as iff) both conditions in the statement are true. Okay, so effectively we end up with if(true), so the //dostuff bit of code will be executed. What about if someThing was 0 and someThingElse was 2? Well, you'd have false and true so it would overall be false. The table below shows all the different possibilities for the && operator.

 First Variable Second Variable Evaulates To...
 true true true
 true false false
 false true false
 false false false

You have already seen an example of an operator when using the double = signs in the if statements, and a list of some more operators can be found at http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html if you are interested or want to know more.


next up previous
Next: Arrays Up: The Frightened Freshers Guide Previous: Methods
Tom Carlson 2006-01-10