Control Statements in Java | A simple Java program contains a set of statements that generally contain expressions and end with a semi-colon.
When we run a Java program, at a time, only one statement is executed.
A program written in Java programming language is generally executed by JVM sequentially (one by one) in the order in which they appear.
These statements are called sequential statements. The flow of execution takes place from top to bottom.
There are three main types of flow of execution (control) that occur in any computer programming. They are:
- Sequential flow
- Conditional or selection
- Repetition or loop
1. Sequential: Statements execute from top to bottom one by one.
2. Conditional or selection: Out of two instructions, only one will be executed successfully based on the specified condition. This is because the condition generates the result as either true or false.
3. Repetition or loop: Group of statements repeats whenever the specified condition is true.
Look at the below figure to understand three types of control flow in computer Java programming.
What are Flow Control Statements in Java?
Flow control statements in Java 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.
For example, 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 conditional control statements in Java. Control statements are also called control structures or control constructs.
Java supports two kinds of control flow statements:
- Conditional statements
- Unconditional statements
The whole classification of control flow statements or decision-making statements is shown 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 specifies the end of a block. It is used to define the scope of variables and support nesting.
Statements contained within a block is known as embedded statement in java.
Conditional Branching or Conditional Statements in Java
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 in Java.
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:
The conditional control flow statement uses the boolean expression for conditional tests. If there is only a single statement in the conditional control flow statement, braces are optional.
Braces are mandatory when there is more than one statement within conditional control flow statement. All conditional control flow statements will be explained in more detail in the further tutorials.
Unconditional Statements in Java
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 in Java.
The following unconditional statements are available in Java:
Hope that this tutorial has covered almost all important points related to Java conditional and unconditional control flow statements, conditional branching. I hope that you will have understood this topic and enjoyed it.
In the next tutorial, we will learn if statements in Java with various example programs. If you find anything incorrect in this tutorial, inform our team through email. Your email will be valuable for us.