💎Common If Statements

Here's a list of common if statements that are often used in interviews:

Check if a number is positive or negative:

int number = 10;
if (number > 0) {
    // Code block for positive number
} else if (number < 0) {
    // Code block for negative number
} else {
    // Code block for zero
}

Check if a number is even or odd:

int number = 7;
if (number % 2 == 0) {
    // Code block for even number
} else {
    // Code block for odd number
}

Check if a number is between a range:

int number = 25;
if (number >= 10 && number <= 50) {
    // Code block for number within the range
} else {
    // Code block for number outside the range
}

Check if a character is a vowel or consonant:

char ch = 'a';
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
    // Code block for vowel
} else {
    // Code block for consonant
}

Check if a string is empty or not:

String str = "Hello";
if (str.isEmpty()) {
    // Code block for empty string
} else {
    // Code block for non-empty string
}

Check if an array is empty or not:

int[] numbers = {1, 2, 3};
if (numbers.length == 0) {
    // Code block for empty array
} else {
    // Code block for non-empty array
}

Check if a condition is true or false:

boolean flag = true;
if (flag) {
    // Code block if flag is true
} else {
    // Code block if flag is false
}

Check if a reference is null or not:

String str = null;
if (str == null) {
    // Code block if str is null
} else {
    // Code block if str is not null
}

Check if two objects are equal or not:

String str1 = "Hello";
String str2 = "World";
if (str1.equals(str2)) {
    // Code block if str1 equals str2
} else {
    // Code block if str1 does not equal str2
}

Check if a condition is true, false, or unknown:

boolean condition = true;
if (condition) {
    // Code block if condition is true
} else if (!condition) {
    // Code block if condition is false
} else {
    // Code block if condition is unknown
}

Check if a string contains a specific substring:

String str = "Hello, World!";
if (str.contains("World")) {
    // Code block if str contains "World"
} else {
    // Code block if str does not contain "World"
}

Check if a number is divisible by another number:

int number = 15;
int divisor = 3;
if (number % divisor == 0) {
    // Code block if number is divisible by divisor
} else {
    // Code block if number is not divisible by divisor
}

Check if a number is within a specific range:

int number = 20;
int lowerBound = 10;
int upperBound = 30;
if (number >= lowerBound && number <= upperBound) {
    // Code block if number is within the range
} else {
    // Code block if number is outside the range
}

Check if a character is uppercase or lowercase:

char ch = 'A';
if (Character.isUpperCase(ch)) {
    // Code block if ch is uppercase
} else if (Character.isLowerCase(ch)) {
    // Code block if ch is lowercase
} else {
    // Code block if ch is not a letter
}

Check if an object is an instance of a specific class:

Object obj = "Hello";
if (obj instanceof String) {
    // Code block if obj is an instance of String
} else {
    // Code block if obj is not an instance of String
}

Check if an array contains a specific element:

int[] numbers = {1, 2, 3, 4, 5};
int target = 3;
boolean found = false;
for (int num : numbers) {
    if (num == target) {
        found = true;
        break;
    }
}
if (found) {
    // Code block if target is found in the array
} else {
    // Code block if target is not found in the array
}

Check if a file exists:

import java.io.File;

File file = new File("path/to/file.txt");
if (file.exists()) {
    // Code block if file exists
} else {
    // Code block if file does not exist
}

Check if a condition is false:

boolean condition = false;
if (!condition) {
    // Code block if condition is false
} else {
    // Code block if condition is true
}

Check if a string starts or ends with a specific substring:

String str = "Hello, World!";
if (str.startsWith("Hello")) {
    // Code block if str starts with "Hello"
} else if (str.endsWith("World!")) {
    // Code block if str ends with "World!"
} else {
    // Code block if str does not start with "Hello" and does not end with "World!"
}

Check if a number is zero or non-zero:

int number = 0;
if (number == 0) {
    // Code block if number is zero
} else {
    // Code block if number is non-zero
}

Last updated