Java If-Else

Lets understand java’s if else programs which will help  you understand if else in java

1)Java program to check whether you are eligble for voting or not.

public class checkAge


public static void main(String arg[])


Scanner sc = new Scanner(;

int age=sc.nextInt(); //Taking input from the user


{System.out.println(“You are elgible for voting in India”);}


{System.out.println(“You are not eligble”);}



Here if else so easy as you can see.

2)If -else if ladder

Java program to

Given an integer n perform the following conditional actions:

  • If is odd, print Weird
  • If is even and in the inclusive range of 2 to 5 , print Not Weird
  • If is even and in the inclusive range of 6 to 20 , print Weird
  • If is even and greater than 20 , print Not Weird

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

Scanner sc=new Scanner(;
int n=sc.nextInt();
String ans=””;
ans = “Weird”;
else {
if(n>=2 && n<=5) { ans= “Not Weird”; } else if(n>=6 && n<=20)
ans =”Not Weird”;
//Complete the code



Feel free to comment to you have doubt regarding the program.


2 thoughts on “Java If-Else

  1. Alex says:

    I’m not positive where you’re getting your information, however good topic. I must spend some time finding out more or understanding more. Thanks for excellent information I used to be searching for this information for my mission.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.