DEV Community

loading...
Cover image for Basics of Java #11

Basics of Java #11

Chathu Rashmini
An individual who loves programming :)
・4 min read

This article is based on the areas comparison operators, logical operators, and conditional operators.

Comparison Operators

While coding you may need to compare two numbers to check whether those two numbers are equal or not. To check whether they are equal, we have to use two equal signs (==). And to check if they are unequal we have to use exclamation mark followed by an equal sign (!=). Try out the following code segment.

int x = 10;
int y = 20;
System.out.println(x == y);
System.out.println(x != y);
Enter fullscreen mode Exit fullscreen mode

At one glace you can see that x and y are not equal at all. Therefore, this code gives output as 'false' and 'true' for the given two println() lines.

Logical Operators

You may want to know whether a given mark of a subject is in a particular range (Is 34 in between 10 and 50?). For that you can use logical operators. There are several logical operators like and operator (&&), or operator (||), and not operator (!). In a case where we want all the conditions to be true we use 'and operator'. If satisfying just one condition is enough, then we can use 'or operator'. 'Not operator' is used to reverse a Boolean value. Let's go through below code segment.

int temp = 22;
boolean isWarm = temp > 20 && temp <30;
System.out.println(isWarm);
boolean mayWarm = temp <15 || temp > 20;
System.out.println(mayWarm);
boolean notWarm = !(mayWarm);
System.out.println(notWarm);
Enter fullscreen mode Exit fullscreen mode

The above code will first print 'true'. That is because the value of the variable temp (22) is in between 20 and 30. The next line will also print 'true' because, though temp is not less than 15 it is greater than 20. So one condition is satisfied. Since we have used 'or operator' there, it gives true. Next line will print 'false'. The variable notWarm is assigned with the reversed value of the variable mayWarm. Since mayWarm is true, notWarm gives false.

Conditional Operators

This operator is to check whether conditions are true or false. Assume there is an institute which gives grades A, B, C, and F according to the marks students have scored.
'If' a student has got marks in the range [75, 100], he is given an 'A'.
'Or else if' his score is in the range [50, 74] he gets a 'B'.
'Else if' the score is in the range [25, 49] a 'C' is given.
'If not' the student gets an 'F'. So how can you code this? Go through the below code.

int marks = 88;
if (marks >= 75 && marks <= 100) {
    System.out.println("A");
    System.out.println("You are a genius!");
}
else if (marks >= 50)
    System.out.println("B");
else if (marks >= 25)
    System.out.println("C");
else
    System.out.println("F");
Enter fullscreen mode Exit fullscreen mode

Let's go through above code line by line. To give an 'A', marks have to be in the range 75 to 100. Therefore, both of the conditions ('75 or greater than that' and 'less than 100 or 100') have to be true. That is why we have to use and operator here (&&).
After that we need to check conditions for a 'B' pass. Marks have to be '50 or greater than 50' and 'less than 75'. Here we need not to check the condition 'less than 75'. That is because the first 'if' condition filter out any mark which is less than 75. So that condition is guaranteed by first 'if' statement. So we are only going to check whether the mark is above or equal to 50. Same process goes to 'C' grade as well.
Then we are remained with the last condition. If marks are less than 25 we have to give an 'F'. You know that the 'else if' condition which gives 'C' pass filter out any mark that is less than 50. So this condition is already guaranteed. Therefore, we need not to write any condition here. Just writing the 'else' is enough.
You may have noticed that I have only used curly braces '{}' for the first 'if' statement. Why is that? As you can see, I have written more than one code lines inside that 'if' condition. Hence we need to add curly braces to let java know which code lines comes under that particular condition. However, in other two 'else if' and 'else' conditions I have only written one code line. In such cases there is no any need of using curly braces. But using indentation in such cases is very important. It also makes your code looks clean and tide.
Change the value of the marks variable into different values and check different outputs you get.

You also can have nested conditional statements. For example, you can have another if condition inside of one if condition.

Task

Ask the user to input his salary. Check if he has entered a negative value. If not add a bonus of 20,000 into his salary. Now check whether the total salary is greater then 90,000. If so print 'Maximum salary range'. If not check whether the salary is in between 90,000 and 50,000. If so print 'Moderate salary range'. Finally if the salary is 20,000 print 'Bonus only'.

Does this conditions checking thing has to be this much of a trouble always? Like if you want to check whether a number is greater than 100 or not, do you really have to struggle with if and else statements? Not at all! There is a much easier way by using Ternary operator (?).
In the given code I am going to check if a number is greater than 100. And if so I will print 'Greater then 100' else I will print 'Less than or equal 100'.

int number = 163;
String nmberGroup = number > 100 ? "Greater then 100" : "Less than or equal 100";
System.out.println(numberGroup);
Enter fullscreen mode Exit fullscreen mode

This is it? Yes! Precise, accurate and less code lines as well. number > 100 checks whether the number is greater than 100. If that condition is true, the first line after the ternary operator (?) gets printed. If it is not the case (which means if number is less than or equal to the 100), the second line (life after the colon : ) gets printed. Try to use this most often than huge if else code blocks, because this is more handy and makes your code neat.

Let's learn about loops in our next article :)

Discussion (0)

Forem Open with the Forem app