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
3. nested if statement
4. if-else statement
5. if-else if statement/ ladder if statements
6. switch statement
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.
In a programming language uses control statements to control the flow of execution of 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 statement:
- if statement: It is a simple decision-making statement. It is used to decide whether the statement or block of statements will be executed or not. 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 statement: Nested if statements mean an if statement inside an if statement. The inner block of if statement will be executed only if the outer block condition is true. For more details.
- if-else statement: In 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. An if-else statement, there are two blocks one is if block and another is else blocked. If a certain condition is true, then if block will be executed otherwise else block will be executed. For more details.
- if-else if statement/ ladder if statements: 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 be executed from the top down. During the execution of conditions if any condition founds true, then the statement associated with that if it is 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.
- switch statement: 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.