Getting familiar with Java:: Arithmetic operator and if statements

In the previous articles we learnt about data variables integer and the arithmetic operator of addition(+). Apart from addition there are some other arithmetic operator such as subtraction (-), for multiplication (*), for division (/),for modulus (%).  The modulus sign is used to find the remainder. In short it is called as mod. Here is the example of this.

Arithmetic Operator:

Here is the code:

 

package day1.examples;



public class MyFirstClass {



                public static void main(String[] args) {

                                // TODO Auto-generated method stub

           System.out.println("Hello World");

           // My first statement

           // addition

           int x = 10;

           int y = 20;

           int result = x + y;

           System.out.println("result= "+result);

           // mod

           int a = 10;

           int b = 3;

           int mod = a % b;

           System.out.println("mod = " +mod);

       

          

                }



}

 

Here, we add two integer data types as a and b. Declare it and provide value to integer a as 10 and integer b as 3 and then in the third declaration we provide int mod which means we provide the modulus function and then declare a % b  where the percentages sign is the sign of modulus. Modulus means remainder of division of integer a with b. When we make this division of 10 to 3 the remainder is 1. So as we declare that in terms of writing system .out.println and then use concatenation method such as providing the mod = within inverted comma and then +mod means the result remainder.  Here is the output of the code when we run with Java.

 

Hello World

result= 30

mod = 1

 

So, now we see at the console window of Eclipse the output is mod = 1 which is true as we have found that while dividing 10 with 3 the remainder or mod is 1. So, signs of other arithmetic operator are increment (++), decrement (__), addition assignment (+=), subtraction assignment (-+) and last not the least is multiplication assignment (*=).

 

“If-else” condition:

Now, first we should look at the if statement and how it is written and what are the elements to be included within it. First go to package explorer of Eclipse and there you can always expand and shrink the packages. Our package folder is day1.examples. It should be there inside src folder under FirstProject inside package explorer. Right click on day1.examples.Then go to new and then to class and it should open another window namely, new java class.

Inside New Java Class, go to Name and there write ‘Exampleif’. Then under ‘Which method stubs would you like to create’ select the first one namely, ‘public static void main ’ and then click on ’Finish’.

 

Now on the code explorer window of Eclispe we have two tabs one is of ‘MyFirstClass.java’ and the other is of ‘Exampleif.java’ and both of these files are signs of class which is inside ‘day1.examples’ folder or package of package explorer which can be seen inside ‘day.examples’ packages folder.

After clicking finish now we have some java code with methods and class can be seen in it and it is as follows.

 

package day1.examples;



public class Exampleif {



                public static void main(String[] args) {

                                // TODO Auto-generated method stub



                }



}

 

Here, you can delete the comments segment which is prefix with //.  Let us delete ‘// TODO Auto-generated method stub’ as this will not be needed and ours code will be as follows.

 

package day1.examples;



public class Exampleif {



                public static void main(String[] args) {

                               



                }



}

 

First if statement:

After the method that is public static void main we will like to write two data types with variables and their value and then the if statement.

int x = 20;

int y = 20;

Here, int is data types and x and y are variables and then value of data type integer variables of both x and y are 20. Now the next statement of if condition.

if (x < y) {

                                                System.out.println("x is less than y");

                                }

In this piece of code we have written that if x is less than y and then the system printout function to print on console is that ‘x is less than y’. If in this situation where the value of  x and y are same then this will not provide any thing on the console. Then we can put the second if statement of ‘s is equal to y’.

if (x == y) {

                System.out.println("x is equal to y");

        }

In this if statement we have written if x is equal to y and in java it is written as if x is == y, here we will provide two == signs for equal as this is the arithmetic operator of equal. Then system.println to provide ‘x is equal to y’.

Now, let us see after x is less than y and then x is equal to y and now if statement about x is greater than y.

if (x > y) {

                System.out.println("x is greater than y");

        }

Now, in the third possible if statement we do provide option for x is greater than y. Here the symbol of greater than is > this one and then system.println and here we within inverted comma wrote x is greater than y.

Now, after method public static void we do provide two variables x and y with the same values such as 20 and then we have three if statement rolling out from this:

If

x<y that is x is less than y

if

x==y that is x is equal to y

and last not the least one is

if

x>y that is x is greater than y.

Now we should save this and then right click any where at the code and then run as and then run java application and the result will be “x is equal to y” and this means that the value of x and y are equal and that is why the second if statement is written in console and other two statement just stopped. The important of using the if statement is that even if some part of if statement does not values with the variable value of data types still it runs the console to find out what is exact value of the variables and comes with the one of the multiple choice among these example.

 

This checks if x is less that y which is not as the value of both variables are same, then it checks the second variable that is if x is equal to y and here the values are same so this if statement matches with the output console. So, it will print x is equal to y as it is correct as values of both are same. Then it comes to third if statement to find out if x is greater than y which is not so this if statement will also not be publishing to the console. So, we have learnt the if statement.

Here, subsequently by changing the values of x and y you can see other if statements in action at console. Suppose you make x is 20 and y is 19 and then the console will print that x is greater than y and if you make x is 19 and y is 20 then the console will print as x is less than y. Of course this can be done but as it seems it is not an efficient way to write the java program as it can be lengthier as well as cumbersome. Because the program is checking if x is greater than y and then if x is equal to y and if x is greater than y so the program while compiling the print function to console is checking all the three statements which makes the program running cumbersome.

So, in the next tutorial you will learn about else statement to make it more efficient and faster so that program runs with fast speed.

This entry was posted in Java on by .

Modified: 27th Oct 2017

About Mohan Manohar Mekap

Mohan Manohar is a blogger from India who founded Ittech back in 2007. He is passionate about all things tech and knows the Internet and computers like the back of his hand.