You will find conditions in Java familiar if you are used to using boolean conditions in C/C++. However, noticed that I said boolean conditions. That is because Java wants boolean conditions, those that generate a true or false answer, instead of the C/C++ conditions which actually look for an integer value.
Notice the following below in how it could work in C/C++ for you, versus how it needs to be written in Java.
// works in C/C++ but not Java
int x = 1;
if(x) {
// the true block
}
// now if works in Java too
if( x == 1 ) {
// true block
}
While extremely similar, there are ever so slight variations as you notice, in that you need to test if x equals one, instead of checking to see if the value is non-zero.
Overall, this makes the code a little easier to read, and less likely to have potential logic errors.
Conditional Operators
The standard conditional operators in C/C++ exist in Java.
Operator | Description |
---|---|
== | Equality operator |
!= | Not Equal to operator |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
If Statement
As you might have noticed above, the if statement is written practically identical to C/C++.
You start with your if
keyword, and put your Boolean condition in parenthesis. The body block of the true statement is located within a set of braces.
Else Statement
The else
statement block is identical to the C/C++ else block. You have your else keyword, and then the else, or false, body block contained within braces.
if(x > 10) {
// true block
} else {
// x <= 10
}
Nested If
Within an if, or else, block statement, you can write another if statement. That way you can check to see if the first statement is true, then do a follow on conditional check, like in the example below:
// assume variables used have been created and are set to a value
if(age > 18) {
if(regsisteredToVote) {
// regsisteredToVote is a boolean, which means it can be it's own test.
}
}
Boolean Logical Operators
Of course, to make things easier than using nested if statements, you might prefer to use Boolean logical operations for and (the double ampersand – &&
) and or (the double pipe – ||
) statements within a condition.
Consider the rewritten example from above:
if(regsisteredToVote && age > 18) {
// true block
}
Because there was no code outside of the internal condition a logical and operator makes sense to use.
Java uses what is known as shortcut logic, so with an and operation, it will test until one of the operations is false, then the whole thing is false, so it stops testing the conditions. Therefore, it makes sense to put the condition most likely to be false first. This is especially true if you will have many conditions to check like in the example below:
if(condition1 && condition2 && condition3 && ... condition_n) {
}
While your code would obviously look a little different, you could potentially have a dozen or more conditions where you are checking boolean values, as well as boolean operators.
The Boolean Or (||
) will work very similarly. Except instead of saying that all conditions have to be true, just one condition has to be true. This means that you will want to put your condition most likely to be true first, to take advantage of the short cut notation.
if(condition1 || condition2 || condition3 || .... || condition_n) {
// only one of the conditions has to be true for the true block to run
}
Conditions in Java was originally found on Access 2 Learn
2 Comments
Comments are closed.