Control Flow And Control Statements

A program can be viewed as a set of instructions. These instructions are executed sequentially by the Java interpreter. The execution starts from the first statement of the script and continues till it reaches the end.

The term 'control flow' in programming refers to the sequential order in which the code is executed during the program run. In any program, 'control' flows from one instruction to the next. This 'flow of control' can be changed or directed accordingly.

It's possible to direct the interpreter to execute specific instructions or continuously repeat a series of instructions when designing logic in the program code. We achieve this by using control statements in the program code=

Control statements

Control statements control the sequence or 'flow' of execution of a program script. Java has three types of control statements:

  1. Decision-Making Statements
  2. Loop Statements
  3. Jump Statements

The decision-making and loop statements are also called conditional statements as they operate according to a condition.

Decision-Making Statements

Decision-Making statements direct the flow of control based on a condition. The Java interpreter evaluates a boolean expression and selects a block of code to execute based on its result. Java provides the following decision-making statements:

  • if statement
  • if-else statement
  • switch statement

Loop Statements

Loop statements are used to execute a specific block of code continuously. The duration of code execution in a loop depends on a particular condition. The statement evaluates this condition and repeatedly executes itself along with its associated block of code as long as the evaluated expression returns true. Java mainly provides three loop statements.

  • for statement
  • while statement
  • do-while statement

Jump Statements

Jump statements move the control flow from one position to a specific statement in another section of the code. Java has two types of jump statements.

  • break statement
  • continue statement

Unlike C/C++, Java does not support the goto statement.