Getting familiar with Java: And && Or II statements

In this article let us learn about and && or II statements in Java. Let us create example and or class. In earlier article I told how to create the new class. Open Eclipse and then go to FILE, NEW and then CLASS. Now you have a new window namely, NEW JAVA CLASS, and in its NAME column write as ExampleAndOr and then at WHICH METHODS STUBS WOULD YOU LIKE TO CREATE then click on PUBLIC STATIC VOIDD MAIN and then at last click on FINISH to create ExampleAndOr.java inside folder day1.examples. In java folder is known as packages. Now, we have following auto-generated methods with classes.

package day1.examples;

public class ExampleAndOr {

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

In this ExampleAndOr.java file the first line says

package day1.examples;

It is s statement, and the package or the entire java file is located at day1.examples folder. You can move to the installation directory of Eclipse to know more about it. There, inside day1.examples folder you can find this java file.

The second line says

public class ExampleAndOr {

This is the name of class which is ExampleAndOr and the curly brace is there and the closing curly brace can be found while clicking on opening curly brace. Then, the auto generated method which is automatically done while creating classes. So this is the base of this java file and now we will add some integers in order to understand more and more complicated forms of statements.

Now let us write the java codes of and (&&) and or (||). One thing is to remember is that while writing and function one need to write two && amperscent symbols. For or too one needs to write two || symbol. This symbol of or || is located right above enter and place shift and then press this key which is just above enter to write this or || symbol. Following is the code and then I will explain in detail about this code.

package day1.examples;

public class ExampleAndOr {

public static void main(String[] args) {

// intial declaration of integers

int x, y;

x = 10;

y= -10;

//&& and

if(x > 0 && y > 0){

System.out.println(” Both nums are +ve”);

// || or

}else if(x > 0 || y > 0) {

System.out.println(” at least one num is +ve”);

}else {

System.out.println(” Both nums are -ve”);

}

}

}

Now let us learn about this code one by one. In earlier para graph we learn that first package name or folder name and then class name and then method name and here then we will write about how to write and or statements. We have taken two integer and first the declaration of integers which is

int x, y;

Then, comes individual value addition to these integer variables of x and y. So, now we declared x is equal to 10 and y is equal to -10 and the declaration of integer is as follows.

x = 10;

y= -10;

Both of these above codes are statements and that is why there is semicolon at the end of both x declaration and y declaration.

Now, the if statement begins. If within brackets x is greater than 0 then two && means and y is greater than 0 then print “Both nums are +ve”. In this example here we consider id x is greater than 0 and y is greater than 0 then write both nums or numbers are positive. So, it is as follows. Here, print function needs both the conditions to be true so that then the print function comes in to existence.

//&& and

if(x > 0 && y > 0){

System.out.println(” Both nums are +ve”);

}

Here, two && are signs of and and then in the next else if statement we can write the or statement. This means either one has to satisfy the condition in order to compiler to print out the print function. This is as follows.

else if(x > 0 || y > 0) {

System.out.println(” at least one num is +ve”);

}

Here among both the conditions the prime factor is the presence of or and this means only one condition has to be met to compile the print out function.

Now, in this two cases of and and or statements, where we do find that the presence of two observations which is in the case of and && we have both x and y needs to above 0 for the compiler to print out the function. In the case of or we have either of x or y which might have been above 0 then it can validate compiler to print out the function.

In the and function if it satisfies both numbers are positive. In the case with or || functions here either one if satisfies the case then the print function works and then we have last not the least is the else function where we do have compiler print function such as both numbers are negative. This is as follows:

else {

System.out.println(” Both nums are -ve”);

}
Further let us analyze further the and or &&. When there is two && signs in any declaration just like the following

if(x > 0 && y > 0)

First we have to check it out the first condition that is x > 0, then we do not have to look further to second condition and then it checks out the second condition if the first condition is correct and if the first condition is not satisfying then it will not check for second condition. If instead of following code is there then,

if(x > 0 & y > 0)

Here at one & means even if the first condition that is x > 0 does not satisfy then also it goes on to check for the second condition that is y > 0. So this is the shuttle difference and most of times these one or two & symbols can be used depending upon the situations of code as well as that of to reduce the code pressure. When we use two && for and statement, when the first condition does not satisfy then it will not go to check for the second condition and instead it will move to else if condition. So, while learning about code infrastructure you will learn more and more about such aberrations where you will learn to reduce pressures on the codes completely. After running the compiler code we got the following print compile function as follows.

at least one num is +ve

Here the code looks for if statement where both the number has to be positive which is not and then it goes to else if statement where either one of the number has to be positive and it satisfies so it print the or statement that is at least one num is +ve and the loop does not need to go to the last else statement in this case.


Discover more from ITTECH

Subscribe to get the latest posts to your email.

freewarespace

Blogger by Choice
Close Menu

Discover more from ITTECH

Subscribe now to keep reading and get access to the full archive.

Continue reading

Close Panel