Episode 8: The if .. else statement
In this episode I will show you one of the most common uses of the boolean type: the if .. else statement. It will be an occasion to see the relational operators that apply to numbers and return a boolean result. Open Eclipse and let’s go.
The if .. else statement
Look at the following program :
1 2 3 4 5 6 7 8 9 10 11 12 | public class Main { public static void main(String[] argv) { int n= 56; if(n > 0) { System.out.println("n is positive"); } } } |
The new thing here is the if instruction. It is quite simple: you are telling the compiler that if n is strictly greater than zero then print “n is positive” on the screen. And since n here is equal to 56 the result of the execution is :
n is positive |
But what if n was negative ?
1 2 3 4 5 6 7 8 9 10 11 12 | public class Main { public static void main(String[] argv) { int n= -36; if(n > 0) { System.out.println("n is positive"); } } } |
In this case the result of execution becomes:
Nothing is displayed on the screen because the condition of n > 0 was not satisfied (-36 is not greater than zero ). Now, if you want to print an appropriate message on the screen when n is negative you can always add another if statement :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Main { public static void main(String[] argv) { int n= -36; if(n > 0) { System.out.println("n is positive"); } if(n < 0) { System.out.println("n is negative"); } } } |
And the result now is :
n is negative |
Or if you want to handle the case of zero :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Main { public static void main(String[] argv) { int n= -36; if(n > 0) { System.out.println("n is positive"); } if(n < 0) { System.out.println("n is negative"); } if(n == 0) { System.out.println("n is equal to zero"); } } } |
Notice the use of == which is an operator for testing equality and not an assignment operator.
The Java language has also a the else instruction which can optionally follow an if instruction. Here is an example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Main { public static void main(String[] argv) { int money = 999999; if (money == 1000000) { System.out.println("money is equal to $1M"); } else { System.out.println("money is not equal to $1M"); } } } |
What is inside the else block (between { and } ) is executed only if the if condition is not satisfied. In the above case the result of the execution is :
money is not equal to $1M |
you can even have nested if .. else :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Main { public static void main(String[] argv) { int money = 999999; if (money >= 99999) { System.out.println("You have a lot of money"); if (money >= 1000000) { System.out.println("You really have a lot of money"); } else { System.out.println("You have less than $1M"); } } else { System.out.println("Money is not everything ;)"); } } } |
The result of the execution is :
You have a lot of money You have less than $1M |
The reason is that the number 999999 satisfies two conditions: it is greater than or equal to 99999 and strictly less than 1000000.
Relational operators
Relational operators are operators used to compare numbers. They are of the number of six :
- == equal to (Be careful: = means assign while == means is equal to)
- != not equal to
- > greater than
- >= greater than or equal to
- < less than
- <= less than or equal to
Here are some concrete examples of using these operators.
You can see that these operators are used in the form: a op b where a and b are numbers and op is one of the six operators. The result of such a op b expression is always of type boolean: true or false. Thus they all can be used as the condition of an if statement.
That’s it for today :). See you a in the next episode