give 10 java programs using if statement
Answers
Answer:
Java if (if-then) Statement
The syntax of if-then statement in Java is:
if (expression) {
// statements
}
Here expression is a boolean expression (returns either true or false).
If the expression is evaluated to true, statement(s) inside the body of if (statements inside parenthesis) are executed.
If the expression is evaluated to false, statement(s) inside the body of if are skipped from execution.
How if statement works?
How if statement works in Java?
Example 1: Java if Statement
class IfStatement {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("Number is positive.");
}
System.out.println("This statement is always executed.");
}
}
When you run the program, the output will be:
Number is positive.
This statement is always executed.
When number is 10, the test expression number > 0 is evaluated to true. Hence, codes inside the body of if statements are executed.