Java If Else Statement | Java Switch Case Example
While learning new things, I realized I haven’t covered up a lot of basic stuff. For instance, Java If Else Statement. Now that I think of it, I haven’t touched the rest of the control statements too.
Hmm, what do you know? I haven’t been a good teacher after all. But hey! I was never one. I consider myself to be your equal. If you have been learning things as they come by, then we are making progress and that’s what matters.
Whoa! I just made an If statement without realizing.
Okay! So this section is entirely related to Java If Else Statement and maybe amble on to Java Switch Case Example to help you realize why the latter’s often used.
Java if else Statement
Alright. we are gonna look at a couple of things here that deal with all kinds of if-else situations you might encounter while working with Java. They come pretty handy when you have a boolean condition to check with literals: true and false.
Remember these statements are also known as Selection Statements, and they are a part of Control Flow Statements.
I have shot them with bullets:
- The If statement
- If-else statement
- If-else-If ladder
One by one we are going to see what they wanna say to us.
Ignore the Raptor. He thinks weird stuff!
If Statement
Moving on quickly like a raptor, I am going to take up the first topic which is fairly simple.
The syntax to remember here is:
if (condition) { then ------stuff-------- }
where replace the stuff with the thing you wish to do of course.
A simple example on Eclipse will help dumb things down for you:
I know it is a tad stupid to think something like that. That’s the only think I could think of in a matter of seconds. Here Daryl and
Here Daryl and dies both have 0 values.
Note: == is a boolean sign that mean “equals”. Our condition is pretty lame when both have been clearly initialized to 0 before. But if the condition is met, it goes into those braces to display the “We riot” clause.
So the output of the above will be:
We riot
Now, what if the condition is not met? In the above code, nothing will happen if we were to say change the instance variable to some other value. Since we didn’t provide any condition when they are not equal, no output will come. That’s where if-else comes into the picture.
If-else statement
Java If else statement comes in handy whenever there are two conditions entailed.
The syntax to follow the Java if else statement is:
if(condition) { -------Do something------- } else { ------Why don't you do something----- }
Let’s just rebuff the above example and start accepting input using the Scanner class. If you have forgotten about how to take inputs you can refer my chapter about Scanner class anytime.
Here’s the code after I turned it to cater Java if else Statement:
I have simply changed the ‘dies’ variable to take inputs from the keyboard.
So now no matter what no. you type you will always get a result. If it isn’t 0, you get “We will keep watching”, and if it is you get “We riot”.
Running the above test now.
But what if your brain is brimming up with conditions? Like there are many conditions and you want to get different results for numerous statements? Don’t worry you can build up a ladder here.
Java if-else-if Ladder
As I said before multiple statements!
The syntax we gotta remember is:
if(condition 1) { -----Do something---- } else if (condition 2) { -----Do something else----- } else if (condition 3) { ----Do something different entirely---- } ... else { ----This should be executed when no conditions are met---- }
In the above syntax the … sign implies you can have n number of else if statements, and hence it is called the Ladder. Replace the conditions 1, 2, 3… and so on with your set of conditions, and you shall be fine.
To see this take form in our code:
I have created three conditions. Well the third one is implied actually. But if you run the above you will be asked to put an input. Any input above than 100 is going to say “Boo!” less than that “We rejoice.” and when it is equal to 100 it will say “We riot”
Go ahead and run it.
This takes us to is there a better way when there is a huge ladder involved?
Yes there is!
It is called Switch.
The Java Switch Case Example
But the choice is yours obviously. You can prefer to use if-else-if since it is much easier to remember. However, Java Switch is technically better since you just have to remember the syntax is all.
Don’t worry with the Java Switch Case Example things will sieve in properly.
Let’s look at the syntax for Java switch first:
switch(expression) { case Value1: ----stuff to be executed---- break; //optional case Value2: ----more stuff to be executed---- break; //optional ... default: ------the final stuff to be executed when no conditions are met------ }
Maybe a good old healthy example will help put things in perspective. We will take the above if-else-if ladder and turn it into a Switch.
Now just enter 95 and see if you get “We rejoice.”
You will!
The issue with Switch is that if you do not use break keyword to apply brakes it is going to break through; you will end up executing all the cases. So if we remove break from the above example, this is what we are going to get:
That’s all you gotta learn from the if else scenario.
Use them whenever there is a true or false situation arising.
Like you want a Beer right now! True!
1 Response
[…] Loops in Java are great when you want a repetitive activity to be carried out till a condition is met. They fall under the control statements category since they let you control the order of execution based on a logic you specify. To get a hang of other control statements you can visit the if else chapter. […]