Explore the English language on a new scale using AI-powered English language navigator.
If statement in JavaIn current article we would like to show how to make choices in the code. There are many situations when a program branches out: if certain condition is true the program goes one way, otherwise it goes another. The choice may be more complicated, when there are more then two variants. If statement is the simplest tool to program choices in the code. To make explanation more illustrative, let's start with the example. Example. Let us test if 4 is equal to 2 + 2. public class IfStatement { public static void main(String[] args) { if (2 + 2 == 4) System.out.println("2 + 2 is equal to 4"); else System.out.println("2 + 2 is not equal to 4"); } } 2 + 2 is equal to 4 Else wayIn the example above you can see the program executing the first statement, because the condition is true. Putting false condition (2 + 2 == 3) in parentheses will make the program executing the second statement. public class IfStatement { public static void main(String[] args) { if (2 + 2 == 3) System.out.println("2 + 2 is equal to 3"); else System.out.println("2 + 2 is not equal to 3"); } } 2 + 2 is not equal to 3 Double vs. single equals signDraw your attention to the fact that we are using double equals sign in condition. It denotes comparison, whereas single equals sing is an assignment operator. Fortunately Java won't compile the code, if you've put the single equals sign by mistake: public class IfStatement { public static void main(String[] args) { if (2 + 2 = 4) // compilation error System.out.println("2 + 2 is equal 4"); else System.out.println("2 + 2 is not equal to 4"); } } More than one statementIf "then" clause contains more than one statement, they must be placed in brackets: public class IfStatement { public static void main(String[] args) { if (2 + 2 == 4) { System.out.println("2 + 2 is equal to 4"); System.out.println("I am serious"); } } } 2 + 2 is equal to 4 I am serious Else branch is optionalNote, that in the recent example there is no else branch. It's not a mistake, using else branch is completely optional. It is possible that you don't need "then" branch or during the code's evolution it becomes unnecessary. You can use empty statement for "then" clause: public class IfStatement { public static void main(String[] args) { if (2 + 2 == 3) ; // empty statement else System.out.println("2 + 2 is not equal to 3"); } } 2 + 2 is not equal to 3 Although it is possible to use empty statement in "then" clause, such technique is considered as a bad programming style. To fix it use reversed condition instead: public class IfStatement { public static void main(String[] args) { if (!(2 + 2 == 3)) System.out.println("2 + 2 is not equal to 3"); } } 2 + 2 is not equal to 3 The condition is interpreted as "if not 2 + 2 equals to 3". Last remarkNote, that condition must be placed in parentheses. Failure to do so will result in compilation error: public class IfStatement { public static void main(String[] args) { if 2 + 2 != 3 // compilation error System.out.println("2 + 2 is not equal to 3"); } } Now, you mastered the last basic Java tool. It's time to get to practice. |