Java Tutorial: If-else Statements

In the last tutorial we left off with getting user input as well as creating variables.  A common thing to want to do with data, whether it is from user input or stored in another way,  is to have the program make “decisions” with the given information.  The reason I put decisions in quotes is because it is up to the programmer to decide what the program will do based off of given information.  But I think this is best seen through an example.  I am not going to set up an entire program for this tutorial, but just do fragments with all the important information.

int num = 10;

if (num < 5 )

{

    System.out.println("The number is less than 5!");

}

else

{

    System.out.println("The number is greater than 5!");

}

 

There we have it, the most basic if-else statement.  Well I guess that isn’t entirely true, the most basic is just an if statement without an else.  And important thing to remember is that you can have an if without an else, but you can’t have an else without an if.  Say that five times fast!

I initialized the integer variable num to the value 10, and I set up the if-else statement accordingly.  For the first part of the statement the condition is num < 5.  If you have knowledge of math symbols you will know that this is read as num is less than 5.

So in this case if the number stored in num is less than 5, the code that follows in the braces will be executed.  However if this statement is false, it will go to the else, and execute the block of codes immediately after it.  In this case which one executes?  If you said the else block then you are correct!  To write it in a more generic way would be as follows:

if (condition)

{

    Some code you want to execute if the if condition is true;

}

else

{

    Execute this code if the if condition is false;

}

 

Pretty simple right?  Now you might be wondering what kind of statements are legal to use in an if statement, so I am going to put them all in a table right here.

Logical Operators in Java

  • <        less than
  • >        greater than
  • <=     less than or equal
  • >=     greater than or equal
  • ==     equivalent
  • !=      does not equal
This should allow you to do any logical comparison against variables you have.  Of course for boolean variables you would only be checking if they are equivalent or not because there is no way to say true is less than false or the other way around.  Another thing you might want to do is have two statements and only do something if they are both true, or at least on is true.  This is referred to as logical and (&&) and logical or (||).  If you put one of these between the two statements in the if declaration it will make the statement true if both sides are true (using and) or if at least one side is true (using or).  Here is an example:
int age = 20;
String name = "Brett";
//using logical and
if (age <= 18 && name == "Brett")
{
    System.out.println("Hello Brett!");
}
else
{ System.out.println("You are not Brett!");
}
//using logical or
if (age <= 18 || name = "Brett")
{
    System.out.println("You are either at least 18 years old or Brett, not sure which though!");
}
else
{
    System.out.println("You are not Brett and you are not at least 18.");
}
 

You can play around with these different logical operators, but if you really thing about it you don’t need to guess in check if you use the proper planning.  There is one more thing that I want to add to this tutorial before ending it and that is that you can do more than just if-else.  You can do if-else if-else.  There can be multiple else if in this statement and I will show you an example that we are all too familiar with.
if (grade <= 90)
{
    System.out.println("You got an A");
}
else if (grade <= 80)
{
    System.out.println("You got a B");
}
else if (grade <= 70)
{
    System.out.println("You got a C");
}
else if (grade <= 60)
{
    System.out.println("You got a D");
}
else
{ System.out.println("You got an F");
}

As you can see there were multiple else if statements, and if one is false, it goes and checks the next statement until it finds the correct block to execute.  That concludes this java tutorial, again if you have any questions feel free to leave a comment and I will get back to you as soon as possible.

Practice Problem
Make an interactive story where the user chooses which path to take, and depending on their choice have different blocks of code execute.  Have fun!
Advertisements

Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: