In the last tutorial, you've learned how to do math in Java (using arithmetic operators). Now, in this tutorial, you'll learn about relational, shorthand and logical operators. Since this tutorial is part of the "How to code in Java" tutorial series, I'll be using Java to demonstrate the operator's functionality. Most of the programming language use the same operators for these operations; therefore, you can implement the knowledge that you've gotten here with another programming language.
Index for this tutorial:
What relational operators are?
Relational operators are operators that lets you compare the values between two values. Then it'll determine their relationship whether the first value is smaller than, greater than, greater, smaller or equal to the second value in term of numeric value or alphabets position in the ASCII table.
There're 6 unique relational operators as listed below:
![]() |
Relational operators in programming |
Now let me use some Java code to demonstrate the operator's operation in action.
![]() |
Example code for relational operators |
Result for the code above |
Relational operators are mostly used with conditional statement where specific code executes when only certain conditions are met. Read this article for more information regarding conditional statements.
What are shorthand operators?
Suppose you still remember in the few tutorials back where I explain about variables. I mention that we can assign new data into the variable using the equal(=) sign or the assignment operator. This sign assigns the value from the right-hand side to the variable that's at the left-hand side. While in the last tutorial, I talked about how you can perform mathematics operation using Java. Ever think if we can store the result of our mathematics operation into a variable? Well, of course, you can! To do that you can do something like this.
Example of doing math operation and storing the result into a variable (long method) |
Or making it to use only two variable:
Doing the same thing as the previous code, but using only two variable. |
But what if I tell you we can make it shorter? Well, that's where shorthand operators or assignment operators come into play. Shorthand operators are operators that perform arithmetic operation along with assignment operation between the left-hand side variable with the right-hand side's variable/value/operations. You can think of it as a shortcut for us to perform arithmetic operations to particular variable while being able to assign/store the result into the same variable(the left-hand side variable) without explicitly perform two operations separately.
Doing the same thing as the previous two examples, but with shorter code. |
These are the available types of shorthand operators for arithmetic operation:
![]() |
List of shorthand operators |
There're other assignment operators available, but that'll be explained in the next tutorial where I explain about the bitwise operator. For the next part, let us get into knowing what's logical operators.
What logical operators are?
![]() |
List of logical operators |
Logical operators example |
Result for the above example. |
These operators also have their precedence with NOT operator being the first. Then, the AND operator is second and the OR operator being the third in the precedence. When there's only one type of logical operators in the compound expression (expression constructed with more than one expression), the result of the expression will be determined by evaluating the expression from left to right. But If there's more than one type of logical operators present in the compound expression, you'll then need to follow the operator's precedence to construct the expression that you want correctly. Take a loke at the example below:
To calculate the result of this expression, we'll start from the NOT operator as it has the highest precedence. By performing the NOT(!) operation on the 6 == 4 expression, the result produced is True as the NOT operator inverses the initial result. You can now see the expression as
3 == 9 || 4 == 4 && True
Then, we'll continue with the AND(&&) operator as it placed second in the precedence of the logical operators. We first see the expression that's at the left side of the operator and then to the right side of it. Since both of these expressions are true, the expression now can be viewed as
3 == 9 || true
Then lastly, we'll finish up the expression with the OR(||) operator. Although the first expression will produce False, the resulted boolean value is True because the second expression is carrying a True value.
Logical operators which are as relational operators are commonly used in conditional statements to determine which blocks of code (section of code) to run, depending on specific conditions.
Well, that's all for this tutorial. I hope you now know what relational, shorthand and logical operators are. As well as how to use them while you're programming. If you find this tutorial useful, please share it with other people that might need it or might find it interesting. In the next tutorial, I'll be explaining to you what bitwise operators are. It's also one of the important components in programming, so stay tuned.
No comments:
Post a Comment