Java If-else StatementThe Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.
Java if StatementThe Java if statement tests the condition. It executes the if block if condition is true. Syntax: Example: Output: Age is greater than 18 Java if-else StatementThe Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed. Syntax: Example: Output: odd number Leap Year Example: A year is leap, if it is divisible by 4 and 400. But, not by 100. Output: LEAP YEAR Using Ternary OperatorWe can also use ternary operator (? :) to perform the task of if…else statement. It is a shorthand way to check the condition. If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned. Example: Output: odd number Java if-else-if ladder StatementThe if-else-if ladder statement executes one condition from multiple statements. Syntax: Example: Output: C grade Program to check POSITIVE, NEGATIVE or ZERO: Output: NEGATIVE Java Nested if statementThe nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true. Syntax: Example: Output: You are eligible to donate blood Example 2: Output: You are not eligible to donate blood |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263852.html