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.
|
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
|
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 |