Java – If Else Block

Else Block: It executes when the given condition of if block fails. Else block is optional for if block. Else block cannot be defined without IF block.

Program to check the input number is Positive or Negative:

import java.util.Scanner;
class Code{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter an integer : ");
		int n = scan.nextInt();
		if(n>=0)
			System.out.println("Positive number");
		else
			System.out.println("Negative number");
	}
}

Check the input number is Even or Odd:

import java.util.Scanner;
class Code 
{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter number : ");
		int n = scan.nextInt();
		if(n%2==0)
			System.out.println("Even number");
		else
			System.out.println("Not even number");
	}
}

Program to Check Person can Vote or Not:

import java.util.Scanner;
class Code 
{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter age : ");
		int age = scan.nextInt();
		if(age>=18)
			System.out.println("Eligible for Vote");
		else
			System.out.println("Wait " + (18-age) + " more years to vote");
	}
}

Program to find the biggest of two numbers:

import java.util.Scanner;
class Code {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter 2 numbers : ");
		int a = scan.nextInt();
		int b = scan.nextInt();
		if(a>b)
			System.out.println("a is big");
		else
			System.out.println("b is big");
	}
}

Program to check the character is Vowel or Not

import java.util.Scanner;
class Code{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter character : ");
		char ch = scan.next().charAt(0);
		if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
			System.out.println("Vowel");
		else
			System.out.println("Not vowel");
	}
}

Program to check the character is Alphabet or Not:

import java.util.Scanner;
class Code
{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter character : ");
		char ch = scan.next().charAt(0);
		if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
			System.out.println("Alphabet");
		else
			System.out.println("Not an Alphabet");
	}
}

Program to Check Login details are Correct or Not:

import java.util.Scanner;
class Code 
{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter user name : ");
		String user = scan.next();
		System.out.println("Enter password : ");
		int pwd = scan.nextInt();
		if(user.equals("logic") && pwd==1234)
			System.out.println("Login Success");
		else
			System.out.println("Error : Invalid login");
	}
}
Scroll to Top