Welcome back to another tutorial for the "How to code in Java" tutorial series. In the previous tutorial, we learned about bitwise operators. In this tutorial, we'll be learning about conditional statements. We'll learn about what is conditional statements, the different type of it and how can we use them. So let's begin.
index for the tutorial:
- So what're conditional statements?
- If, if...else, if...else-if...else statements.
- The switch statement.
- The ternary operator (?).
If you have been following the series, that means you already know how to write a program to print out a specific value and how to use variables. Now, you will see how we can run individual blocks of code when a specific condition is met.
So what're conditional statements?
A conditional statement as it sounds like is a block of codes that will run when certain conditions are met.
If, if...else, if...else-if...else statements
Why is it important, you ask? Well, let us say we want to print out a message according to the age of the customer entering a casino. For starter, we just want to display "You're permitted to enter the casino." to the customer when they have an age greater than or equal to 18.
If you've noticed that our program here will print out the message if only the condition inside the if-statement is met (true). Now let's convert the ideas above into Java code.
Simple if-statement |
the if statement message |
All that an if-statement does is checks whether a condition is true or not, and if the conditions provided is correct, then the code inside the statement block will be executed. If-statement works just like how we use "if" in English. Now, let's convert the code above into English. In English, it would sound something like this, "If the age of the customer is greater than or equal to 18, notify them that they are permitted to enter the casino.". If you don't quite understand what comparison operators (<, <=, >=, >, ==) are, feel free to read this article for more understanding to it.
Next, what happens when the age of the customer is less than 18? The answer is nothing! The program will not output anything. That's because our code will only display a message when the customer age is greater than or equal to 18. What would we need to do if we want to also display a message to notify the user that they are not permitted to enter the casino? This is where we'll be using the else-statement. When we use the else-statement, the code blocks inside it will be executed when the previous if-statement fails to meet its required condition (false).
Simple If-else statement |
the output of the code above |
Next, let's talk about how we can improve the program a little. Imagine that the customer provided a value that's lesser than 0 or a big number? It's not logical for a human to have an age of more than 200 and above (although the oldest person that lived in the world is aged 122 years old) or to have an age of less than 0. What can we do to fix this flaw? This is where we will use logical operators && and the else...if statement. We now can change the code to something like this.
an if-else-if statement with the logical operator |
The else...if-statement will only execute the code block when the previous if statement condition is not true AND the current condition is true. You can use as many else...if-statement as you want in a program, so don't worry if there's any limit in using it. We can interpret the code into English that sounds something like this, "If customer age is less than 18 and greater than 0 notify them that they're not allowed into the casino. While otherwise, if the customer age is greater or equal to 18 and less than or equal to 150, tell them that they're allowed to enter the casino.".
But, how would we let the customer know that they've provided a wrong number? That's when we use another else-statement after the if...else-block. The code in the else-statement block will only be executed when all if and else...if statement's condition does not meet. Now let's see how would we write it using Java.
Simple if-else...if-else statement along with some logical operators |
Result for the code above. |
Now, our code will sound something like this in English. If the age of the customer is less than and greater than 0 tell them that they are not permitted to enter the casino. Otherwise, if the age of the customers is greater than or equal to 18 and less than 150, tell them that they can enter the casino. But if both of these statements are not meet, say to the customer that they entered the wrong value. Note that you really need to understand when does an if, else...if and an else-statement code block will be executed to avoid misconstruction of you condition statement.
The switch statement.
Now that we've done with the if...else...if...else statement let's continue with the switch statement. Imagine that we want to code a program that will print out a different message when the users are celebrating their 1st, 10th, 20th, 30th, and so on until 100th birthday anniversary. Now, I know that many people live more than 100 years old. But for the sake of this tutorial, We'll use 100 as the max-age for a person to live. What would you do to code the program? Well, if you're to use if..else statement for this, you would get something like this.
A very messy code to implement the problem. |
This would be really messy and hard to read, right? How about we make it more readable? Well, we can use a switch statement in this kind of situation. That;'s because we're only comparing the user age with absolute value (1, 10, 20, etc.) and not a range (>1). By changing the code into a switch statement, the code now is more readable, right? One thing to note is that switch cases can only be used with integers, a string or a character. It cannot be used with value with the type of float, double or boolean.
The code that's inside the case block will be executed when one case matches the value, and when it reaches the break statement, the system will then exit the switch statement. If you didn't put a break statement at the end of one of the cases code block, things would get weird really quick. If you didn't put a break statement lets say in the example just now, we forgot to write a break statement in the 10th birthday anniversary. Then right after the system output the celebration message for the 10th birthday anniversary, the system would continue to the 20th birthday anniversary message. Since in the 20th birthday anniversary block, there's a break statement, the system will only print out the message from the 10th, and 20th-anniversary block then exits the switch statement.
The default statements are used to specify the code to be run when no case matches the user's age. In our example, we used the default statement to print out a message to wish the user for reaching their age.
The ternary operator (?)
Lastly, we'll get into the ternary operators. This operator lets us assign a value into a variable by checking conditions and provide value to the variable base on the true or false of the condition. This operator is really helpful if you want to assign a value into a variable base on simple (or complex) conditions really fast.
switch statement example. |
This statement would assign 1000 into the variable result. This is because the comparison of value == 100 is true as the "value" variable has been assigned with the value 100 previously. If let's say we assign the value variable with another value, you'll find out that the value that's been assigned into the result variable became 10. This is because the condition now has become false, making the ternary operator to choose the value that's at the right of the colon sign into the result variable. One interesting thing about the ternary operator is that you can use multiple ternary operators together (nested ternary operator).
Well, that's all for this tutorial. I hope that you now know what's conditional statements and how they work. As always, If you find this tutorial helpful, please share it with other people that might need it or might find it interesting. Another thing is that if you have any question, critics or suggestion that you want to tell me, feel free to leave a comment in the comment section below. In the next tutorial, we'll be going through on how do we take users input in Java terminal program. So stay tuned.
No comments:
Post a Comment