# 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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

```java
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:

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qatesting.gitbook.io/qa/java/conditionals/common-if-statements.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
