Suppose a situation comes in the program, where we need to change the order of execution of statements based on specific conditions or repeat a group of statements until particular specified conditions are met.
Such situations can be achieved with the help of control flow statements or simply called control statements. Control statements are also called control structures or control constructs. So, let’s understand control flow statements in more deep.
What are Control Flow Statements?
Control flow statements are those statements that change the flow of execution and provide better control to the programmer on the flow of execution in the program. These statements are executed randomly and repeatedly. Control statements in java programming are used to write better and complex programs.
Java supports two kinds of control flow statements:
1. Conditional statements
2. Unconditional statements
The whole classification of control flow statements or decision-making statements can be seen in the below figure.
A block is a specific kind of statement that encloses a group of statements between braces. It is represented by an opening brace and closing brace.
The opening brace represents the beginning of block and closing brace specify the end of a block. It is used to define scope of variables and support nesting. Statements contained within a block is known as embedded statement in java.
Conditional Branching or Conditional Statements
A program that breaks the sequential flow and jumps to another part of the code is called branching in java. When branching is based on a specific condition, then it is called conditional branching or conditional statements. Since the flow of execution is transferred on a particular condition from one part of code to another part, it is also called conditional execution.
Java supports the following conditional control flow statement:
1. if …else statement
2. do …while statement
3. while loop
4. for loop
5. for-each loop
6. switch statement
The conditional control flow statement uses the boolean expression for conditional tests. If there is only a single statement in conditional control flow statement, braces are optional.
Braces are mandatory when there are more than one statement within conditional control flow statement. All conditional control flow statements will be explained with more details in the next tutorials.
When the flow of execution jumps to another part of code without carrying out any conditional test, it is called unconditional statements or unconditional execution. The following unconditional statements are available in Java:
1. break statement
2. continue statement
3. return statement
Hope that this tutorial has covered almost all important points related to conditional and unconditional control flow statements, conditional branching. I hope that you will have understood this topic and enjoyed it.
Thanks for reading!