The if statement is an absolute must-know in Java if you want to create programs that don't always perform the exact same routine over and over again.
Programs are always executed from top to bottom. That is just how they work. A computer will read one line of code and then go to the next line. Unless you tell the computer to skip lines or go to other parts of the code given certain conditions, the computer will always proceed to the next line of code.
If you've been practicing writing code from the other tutorials and experimenting, then you're probably wondering, "how can I make the computer perform a different calculation based on some condition?" In the case of input, how can we make the computer do something entirely different based on what the user inputs? The answer is with conditionals.
Conditionals are just conditions. You create a set of conditions and the computer does what it's supposed to based on the condition. Most programming languages I know follow the same set up when using conditionals. In Java, the most basic kind is called the if statement. In order to give you an example, I will need to show you some code. Start up Eclipse or whatever Java editor you use (I highly recommend Eclipse!) Lets create an integer variable and set its value to 3.
Assume that this piece of code is inside of a main method that belongs to a class. In other words, assume this:
The class and main code should look very familiar to you by now. If they don't, you should go back and write your first program.
Okay, so we have an int variable. Now, to demonstrate how to use an if statement. Let's look at an example.
Note that the // lines creates a comment, and everything after it on that line is a comment and is ignored by Java. This makes it easy to add English text so that you can leave some personal notes. Anyway, this is an if statement. Let's look at what it consists of.
You start with if, and if must be in all lower case or Java will not recognize what it is. Next comes a pair of parenthesis. You will place your condition inside of the parenthesis. That's how an if always works, so do not forget this fact! Lastly you play curly brackets at the beginning and the end of the if statement. Everything that you want the computer to do when the condition is met is done within those two curly brackets. That's all there is to it.
Now, we need to know one last detail: how do we create conditions? I'll start with a basic table of conditional symbols that Java knows.
|==||is equal to|
|>||is greater than|
|<||is less than|
|>=||is greater than or equal to|
|<=||is less than or equal to|
These are your basic conditions for primitive types. Remember what a primitive type is? I suggest looking back at the variables tutorial if you have forgotten.
In the example above, I used the "is equal to" condition. My conditional reads, "if num is equal to 3". If it does (and it does, because remember I set it equal to 3) then it will execute whatever code I put in between those brackets. I'll add a simple print statement inside the conditional, and then run the program, so now our program looks like this:
If I run this program I will get back, "Hello world!" from the output. With this same code, change int num = 3 to int num = 4 and run the program. You will notice that it produces no output because it did not pass the condition. Change the conditional now to num >= 3 and you will see that the code works once again.
That's not the end of the story, as there are a few more details about conditionals you'll want to know. I'm going to introduce this next item by giving an example of some Java code. This is only a small extracted piece of code; you have to pretend the rest is already there and working or try it out yourself, which I highly recommend.
If I were to run this code, the computer would output Hello world! twice. This is because num is greater than or equal to three, and it is also just plain equal to four. It passes both conditions so our output is done twice. Now, what if I wanted it to do one condition but not the other? How would I do that? The answer is simple:
Same code, but look carefully; there's an extra word inserted in the second if statement. I added the word else. “Else if” works by tying into the if statement above it. This code now reads that if num is greater than or equal to three, print “Hello world!”. Else, if num is equal to four, print out “Hello world!”. It's pretty simple, and this will only execute the first condition, the second condition, or neither. You can also tie several else if's to a single if. Let's look at a different example.
If num is less than three, then output "Under 3". If that's not true, go to the next condition. If num is equal to three then print out "Equals 3". If num however is equal to or greater than four, then print out "Over 3". Again, you can tie in as many else if's as you want, and they will belong to the first if that's above.
What if you need it to execute a certain piece of code only if none of the conditions are met and the conditions are too complicated to write out? What if you wanted an overall general condition if none of the conditions you specified are met?
There's one last statement you can use, and I'm going to modify the previous code to demostrate it.
Notice how the last part is missing the if. This is what is called an else. This is different from an else if because it will execute only when all other conditions fail. In this case I'm saying that if num is not less than three, and it is not equal to three, then it must be greater than 3, so I just used an else to show that. Unlike else if, you may only use else at the very bottom of the chain of else if's and you may only use it once per chain of if statements. So, you could never have:
This doesn't even make sense in English, and you will get an error if you try to do this.
Again remember, if, else if, and else are all Java keywords. They must be typed in all lower case letters for Java to recognize them. Use these wherever you need the computer to make a decision or to make the computer do something different depending on user input or other variables. That's the end of this simple conditionals tutorial, so experiment with some different conditions and have fun with it!
Try to figure out how this snippet of code works. Assume that a class and main exist and that I have imported what I need for Scanner to work. If you've never seen a Scanner, that is because you did not read the input tutorial. If you have any questions, feel free to contact me and I'll help you out. Good luck!
Leave If Statements and return to Beginner Java Tutorials