|
if ( 10 <= a <= 20 )
System.out.println("yes");
else
System.out.println("no");
|
Because 10 <= a <= 20 is evaluated as follows:
Expression: 10 <= a <= 20
Operators: <= <=
Evaluated as: 10 <= a <= 20 (10 <= a is either true or false)
true <= 20
or false <= 20
|
It is illegal to use the compare <= operator between a Boolean value and a number
if ( 10 <= a && a <= 20 )
System.out.println("yes");
else
System.out.println("no");
|
Because only numbers that are between 10 and 20 will satisfy the condition 10 <= a && a <= 20
import java.util.Scanner;
public class Between01
{
public static void main(String[] args)
{
int a;
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextInt(); // Read in number into a
if ( 10 <= a && a <= 20 )
{
System.out.println("Number is between 10 and 20");
}
else
{
System.out.println("Number is NOT between 10 and 20");
}
}
}
|
How to run the program:
|