If else statement in Java

if else statement in Java

Now, when it is clear that we are going to deal with some decision-based things like we are going to do something on the basis of some condition.

Let’s now consider some examples that now relate to our programming. I mean that you may not want to write a program to check whether or not your teacher gives you a holiday, or whether or not you should go to the doctor on the basis of whether you are sick or not. We are more interested in the implementation part right now, so let’s quickly jump right into it.

But before we move any further, we need to know how can we write this if and else statements in java. If you are a little bit familiar with C or C++ programming, then you might be aware of the syntax already because you are going to find that the syntax for if and else resembles the one from C or C++. But you don’t have to worry if you know nothing about C or C++.

Consider that we take a number from the user, and we check that is the number odd or even. If the number is even, then we have to print that the number is even, else we have to print that the number is odd. This challenge seems to be pretty much easy.

Let’s see how we can proceed into it to successfully complete the challenge. Here are the steps or the approach that we can follow while we write a program for this above question –

  • step 1: Take user input(probably a number)
  • step 2: check whether the number is odd or even. (basically check whether or not the number is divisible by 2)
  • Step 3: if the number is even, print – “The number is even”. Else print – “The number is odd”.
  • Step 4: Do you even need step 4? we are done with the output in the previous step! Enjoy.

So, with the above steps pretty much clear to us, we can now jump to writing the code. You can write the code yourself, but in case you run into any kind of difficulty, you can come back here and have reference to my code which is here –

import java.util.Scanner;

public class HelloWorld {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println(“Please enter a number: “);
int number = scanner.nextInt();
if (number%2 ==0){
System.out.println(“The number is even”);
}
else{
System.out.println(“The number is odd”);
}
}
}

So, in short, this has to say that the if-else block can be used in situations where we need to do something, but that something depends on something. For example, we can declare someone a pass if that someone has got marks above 35. So, there is a condition for passing, which is that you have to get marks above 35.

So, in fact, now you can write a program where you can make the user enter his/her total marks out of 100 just to check that he/she passed or failed. The logic is kind of simple. We just have to get a user input and check if the number that was given as input from the user is greater than 35, and then only the user can be declared as a pass. Let me tell you that I have made an assumption that above 35 marks are passed, and 35 or less than 35 is failed. Or you can consider 35 as a boundary pass. That I’ll keep flexible for you. You may try this example so that you can quickly get your hands over a simple program.