Control Structures in Java
Control Structures in Java
Java provides special blocks of statements that allow great control over the execution of statements in a program. These are referred to as the control structures in Java.
Types of Control Structures
Java supports 5 types of control structures to control the flow of a program.
Sequential Control Structure:
- If the programmer uses no control structure within a Java program, the control moves forward to the next statements sequentially. This scenario is known as Sequential Control Structure.
- By default, all the statements execute sequentially one by one.
Conditional Control Structure:
- We can also call these statements as decision making control structure.
- They let us make a decision based upon the result of a condition.
- These statements will execute a block of code conditionally depending on whether the condition is true or false.
- If the condition is true, then the block of code will execute otherwise it will be skipped.
- In some cases, there are 2 statement blocks one for the true value and other for the false value.
- Moreover, the control will not go to the next statement until and unless the condition is satisfied.
- For example,
Repetition Control Structure:
- The looping statements help to execute a set of instructions repeatedly as long as the condition is true.
- It repeats some portion of the program either a specified number of times or until a particular condition is satisfied.
- This process of repeating some portion of the program is called looping or branching.
- Sometimes we will provide the number of repetitions whereas in some cases we may not be aware of the number of repetitions.
- For example:
Selection Control Structure:
- This control structure allows us to make a decision from a number of choices.
- Mostly, menu-driven programs use switch cases, where we have to choose a relevant option out of multiple options.
- Each option has a different function to perform.
- For example:
- the switch case
Jump Control Structure:
- There are 2 jump control statements i.e. break and continue.
- These statements directly transfer control of the program without depending upon any conditions.
- They cause a sudden jump or control transfer from the current statement to another statement somewhere else in the code.
- Thus they can interrupt the normal flow of the program according to the need.
In the next article, I will discuss in details all the types of decision making statements and conditional control structures available in Java. The article will cover all the types of if..else statements and simple programs based on if..else statements.