## Java TUTORIAL - if else

ReferenceDesigner.com tutorials are different. We do not follow a bookish way of learning in a certain pattern. We instead present the matters that HELPS you learn the language AND the conepts and in a non linear fashion. That is why we present the matters in a slightly different order than those presented by books or other online tutorials.

The if else statement is the fundamental building block of any programming language. It is the basic decision making statement that gives a programming language ability to code and run an algorithm.

Let us now write a java code which will compare a given variable against a constant number and print the result accordingly.

 `/*  ReferenceDesigner.com Tutorial for beginners  If statement  Example Program showing use of if else statement*/class ifelse {  public static void main (String args[]) {  int x = 48 ; if ( x > 50) { System.out.println("x is greater than 50"); System.out.println("Bye !"); } else { System.out.println("x is less than or equal to 50"); System.out.println("Learning Java is easy and fun with referencedesigner.com tutorial!"); } } }`

If you compile and run this program, you will get the output as follows

 ```C:\Program Files\Java\jdk1.7.0_17\bin>java ifelse x is less than or equal to 50 Learning Java is easy and fun with referencedesigner.com tutorial! C:\Program Files\Java\jdk1.7.0_17\bin>```

### Exercises

We believe that the best way to learn coding is by exercises. So here are some exercises for you

Exercise 1

Change the value of x in the above program such that print the first set of statements in place of the second set of statements/

Exercise 2

Define two integer variables x and y and assign them some constant value. Write a console output depending upon whether their sum is greater than 100 or not.

Let us now write another meaningful example that will printout the maximum of three given numbers

 `/*  ReferenceDesigner.com Tutorial for beginners  Eaxmple showing boolean operator   And also != relational operator*/class max {  public static void main (String args[]) {  int x = 5 ; int y = 41 ; int z = 13 ; int max;  if (x > y) { max = x; } else { max = y; }  if (z > max) max = z;   System.out.print("The max of x, y and z is "); System.out.println(max); } }`

As you may have expected, it gives the following output

 ```C:\Program Files\Java\jdk1.7.0_17\bin>java max The max of x, y and z is 41```