The program shows how to Check for the Negative Number in java. This program is also a good demonstration to know the working process of the if-else statement.
If you do not understand the program, perhaps you may want to learn about the if-else statement first.
Java Program to Check for the Negative Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //check for the negative number using if-else statement in java public class IfElseNegNumber { public static void main(String[] args) { int num = 5; if(num < 0) //checking condition for true { System.out.println("Number is negative"); } else //executed if the condition is false { System.out.println("Number is positive"); } System.out.println("Always executed"); } } |
Output:
Number is positive
Always executed