Conditional Control Statements in Java | A simple Java program contains a set of statements that generally contains expressions and ends 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:
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.
What are Control Flow Statements in Java?
Control flow 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:
- if …else statement
- do …while statement
- while loop
- for loop
- for-each loop
- switch 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:
- break statement
- continue statement
- return statement
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.
Thanks for reading!!!
Next ⇒ If Statement in Java