In this section, we will learn about the control statements in Java. Control Statements in Java are also known as conditional statements in java, the fundamentals required for Java Programming.
Here is the table content of the article will we will cover this topic.
1. What are the Control statements?
2. if statement in java
3. nested if statement in java
4. if-else statement in java
5. if-else if statement in java / ladder if statements in java
6. switch statement in java
Control statements in java/Control flow statements in java
In programming, we face some situations where we need to execute a set of statements based on a condition then we need to use control statements.
A programming language uses control statements to control the flow of execution of a program based on certain conditions. If we want to execute some code based on a condition, then control statements are useful. In JAVA we have some control statements:
It is a simple decision-making statement. It is used to decide whether the statement or block of statements will be executed or not. A Block of statements will be executed if the given condition is true otherwise the block of the statement will be skipped. For more details.
Nested if statements mean an if statement inside an if statement. The inner block of the if statement will be executed only if the outer block condition is true. For more details.
In the if statement the block of statements will be executed if the given condition is true otherwise block of the statement will be skipped. But we also want to execute the same block of code if the condition is false. Here we come on the if-else statement. In an if-else statement, there are two blocks one is an if block and another is an else block. If a certain condition is true, then if block will be executed otherwise else block will be executed. For more details.
If we want to execute the different codes based on different conditions then we can use if-else-if. It is also known as if-else if ladder. This statement is always executed from the top down. During the execution of conditions if any condition is founded true, then the statement associated with that if statement will get executed, and the rest of the code will be skipped. If none of the conditions is true, then the final else statement will be executed. For more details.
The switch statement is like the if-else-if ladder statement. To reduce the code complexity of the if-else-if ladder switch statement comes.
In a switch, the statement executes one statement from multiple statements based on condition. In the switch statements, we have a number of choices and we can perform a different task for each choice. For more details.