73.4k views
0 votes
Logical operators can simplify nested conditional statements. For example, can you rewrite this code using a single if statement?

if (x > 0) {
if (x < 10) {
System.out.println("positive single digit number.");
}
}

1 Answer

0 votes

In Java we have these wonderful things called logical operators. They are && and ||. The ampersand signs stand for and, while the straight lines stand for or.

To rewrite your code we can use the ampersand.

if (x > 0 && x < 10){

System.out.println("Positive single digit number.");

}

I hope this helps!

User DirWolf
by
7.1k points