if number is negative java

www.la‮.iruttu‬com
if number is negative java

To check if a number is negative in Java, you can use an if statement with a condition that checks whether the number is less than zero. Here is an example of how to do this:

int number = -10;

if (number < 0) {
  System.out.println("The number is negative.");
}

In this example, the condition number < 0 is evaluated to true, so the code inside the curly braces is executed and the message "The number is negative." is printed to the console.

You can also use an if-else statement to execute different blocks of code based on whether the number is positive or negative. The syntax for the if-else statement is as follows:

if (condition) {
  // code to execute if the condition is true
} else {
  // code to execute if the condition is false
}

Here is an example of how to use an if-else statement to check if a number is positive or negative:

int number = -10;

if (number > 0) {
  System.out.println("The number is positive.");
} else {
  System.out.println("The number is negative.");
}

In this example, the condition number > 0 is evaluated to false, so the code inside the second set of curly braces is executed and the message "The number is negative." is printed to the console.

It is important to carefully consider the conditions and actions in your if and if-else statements to ensure that your code is correct and easy to understand.

Created Time:2017-11-01 22:29:50  Author:lautturi