Describe the if statement in Java ?
All but the most trivial computer programs required to form decisions. They test a condition and operate in a different way based on the outcome of the test. This is quite common in actual life. For example you stick your hand out the window to test if it's raining. If it is raining then you take an umbrella along with you. If it isn't raining then you don't.
All programming languages have some form of an if statement in which tests conditions. In the previous code you should have tested whether there in fact were command line arguments before you tried to use them.
Arrays have lengths and you can access in which length through referencing the variable arrayname.length You test the length of the args array as follows.
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
if (args.length > 0) {
System.out.println("Hello " + args[0]);
}
}
}
System.out.println(args[0]) was wrapped in a conditional test, if (args.length > 0) { }. The code within the braces, System.out.println(args[0]), now gets executed if and only if the length of the args array is greater than zero.
The arguments to a conditional statement such as if must be a boolean value, that is something that evaluates to true or false. Integers are not permissible.
In Java numerical greater than and lesser than tests are completed along with the > and < operators respectively. You can test whether a number is less than or equal to or greater than or equal to another number along with the <= and >= operators.