In this tutorial, we will understand the answer to a question i.e What are Control Flow Statements in Java? We will also focus on their different types as well as their work.
What are Control Flow Statements in Java?
Control Flow Statements in Java are used to control the flow of execution of a program. These statements are special statements that need not be executed sequentially. Every Control Flow Statement has a condition according to which the flow of the program changes.
List of Control Flow Statements in Java
Java has three basic types of Control Flow Statements in Java:
- if statement
- if-else statement
- switch statement
What is if-statement in Java?
if-statement is the type of Control Flow Statements in Java. if-statement is the simplest of all the other conditional statements. It has a condition and a body. The condition is evaluated and then it is decided whether or not the code in the body should be executed or not.
Syntax of if-statement in Java:
if(condition) { //Body } next statement;
At first, the condition is evaluated to get a Boolean result. The Boolean result plays a key role in whether or not the body should be executed or not. If the condition is evaluated as ‘true’ then the body of the if-statement is executed and then it continues to execute the next statement after it. If the condition is ‘false’, then the body isn’t executed and then the next statement after the if-statement gets executed.
This way, the flow of the program is controlled and the key role played here is by the condition.
Example of if-statement in Java:
class Program { public static void main(String agmt[]) { if(2<4) { System.out.println(“2 is less than 4”); } } } /* Output 2 is less than 4 */
In the above example, the condition in the if-statement is 2< 4 which will return a Boolean result as ‘true’ (because 2 is less than 4). This means the condition is evaluated and it is ‘true’, then the body of the if-condition is executed and the console gets printed with “2 is less than 4”.
What is if-else statement in Java?
if-else is the type of Control Flow Statements in Java. The if-else statement is an extended part of the if-statement. It has all the things that an if-statement has but has an extra ‘else’ block.
Syntax of if-else statement in Java:
if(condition) { //Executed when condition is true } else { //Executed when the condition is false }
When the condition in the if-statement gets evaluated as ‘true’ then the body next to the if-block gets executed. In case where the condition gets evaluated as ‘false’ then the body of the else block gets executed.
Example of if-else statement in Java:
class Program { public static void main(String agmt[]) { if(24 == 47) { System.out.println(“24 is equal to 47”); } else { System.out.println(“24 is not equal to 47”); } } } /* Output 24 is not equal to 47 */
In the above example, condition 24 == 47 gets evaluated as “false” and the body next to if-condition is not executed. But we also have an ‘else’ block right after the if block, so the else block gets executed and the console gets printed with “24 is not equal to 47”.
What is a switch statement in Java?
A switch statement is a type of Control Flow Statements in Java. The switch statement is another conditional statement that is also used to control the flow of a program. A switch statement has an expression (which evaluates to a value) and also has its block of statements arranged according to the result of the expression.
Syntax of switch statement in Java:
switch(expression) { case <case_1>: //Code break; case <case_2>: //Code break; ................ case <case_n>: //Code break; default: //Code }
The code inside the switch block is split into cases. When the expression is evaluated in the beginning, it will result in one of the cases that is mentioned inside the switch block. If neither of the cases matches with the evaluated expression, then the statements after the ‘default’ block get executed.
Example of switch statement in Java:
class Program { public static void main(String agmt[]) { switch(4+26+36) { case 65: System.out.println(“The value is 65”); break; case 68: System.out.println(“The value is 68”); break; case 66: System.out.println(“The value is 66”); break; default: System.out.println(“I don’t know what it is”); } } }
The above example clearly shows how the code is split into cases in a switch statement. The expression that is passed into the switch is 4+26+36. This expression, when calculated, will give 66. The value 66 will enter the body of the switch and check if there is a case with the value 66.
In the example, we see the third case matches the outcome of the expression. So, the statements after ‘case 66’ will get executed. In this case, we have a printing statement that prints “The value is 66”.
Note: We also see a break statement at the end of every case. This is placed for a reason. Suppose, the first case matches with the expression of the switch statement, then the code written in the first case will get executed but then if there is no break-statement after that the code after the first case will continue to execute and then the next one and so on. The switch statement will break only if it finds a break statement or if it reaches the end of the switch case.
Thanks for learning What are Control Flow Statements in Java? I hope you like this post. If you are interested to learn these types of posts so stay connected with us.