What is Conditional Statements in Java

In this article we learn what is conditional statements in java and types of conditional statements.

What is Conditional Statements in Java?

1. Conditional statements in java can be used to apply conditions or restrictions on Java statements.

2. Programmer can control the flow of execution of a program by using different Conditional statements.

Types of Conditional Statements in Java

1. Decision making statements

2. Looping statements

1. Decision making statements.

A) if statement

An if statement checks a particular condition; if the condition is true then, it will execute a statement or a set of statements. Otherwise, if the condition is false, it will ignore that statement or set of statements.

B) nested if statement

The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.

C) if-else statement

It executes the if block if condition is true otherwise else block is executed.

D) if-else-if statement

‘if’ statement is also called the ‘if-else-if’ ladder. It checks the first conditional expression, and if it returns false, then it will check the second conditional expression and so on. If all conditional expressions return false, it executes the statement(s) of else part.

E) switch statement

1. A switch statement is a multiple-branch statement in Java.

2. The Java switch statement is used to evaluate a statement against multiple cases and execute code if a particular case is met. Switch statements are a form of conditional statement used to control the flow of a program.

3. We used the break keyword. When Java executes a break statement, it’ll stop running the code within the switch block and continue running the program.

4. The program doesn’t need to evaluate more cases after the right case has been found. We should use break keyword at the end of every case.

5. When no match is found, the default statement gets executed.

6. In the absence of break, the control flow moves to the next case below the matching case, and this is called fall through.

7. Note: The default statement is optional and if it is missing, no action takes place if all matches fail.

2. Looping statements

A) For loop

1. We use for loop statement when we know exactly how many times we want to execute the block of code.

2. Syntax:

For {initialization; condition; increment/decrement) {

// block of statement

}

3. The start of the for loop has three parts:

Initialization is an expression that initializes the start of the loop. If we have a loop index, this expression might declare and initialize it, for example, int i = 0. Variables that we declare in this part of the for loop are local to the loop itself; they cease existing after the loop is finished executing.

Condition is the test that occurs after each pass of the loop. The condition must be a boolean expression or function that returns a boolean value, for example, i < 10. If the test is true, the loop executes. Once the test is false, the loop stops executing.

Increment is any expression or function call. Commonly, the increment is used to change the value of the loop index to bring the state of the loop closer to returning false and completing

B) While Loop

1. The while loop is used to repeat a statement or block of statements as long as a particular condition is true.

2. If the condition is true, then the statement inside the while loop is executed.

3. If the condition is false at the beginning, then the block of code inside the while loop is never executed.

4. We can use Break keyword in while loop statement.

5. Syntax:

while (condition) {

// bodyOfLoop;

}

Other Popular Articles

What is Type Casting in Java?

Leave a Comment