Control Statements in Java

Java uses a control statement to direct the flow of execution of any program based on some condition. It may casue the program :

  • To sometimes divide into branches (if-esle, switch).
  • To iterate over a portion of code again and again (loops like while and for).
  • To skip some portion of code and continue execution somewhere else (break, continue and return).

Types of Control Statements

Like most other languages, Java has three types of control statements:

  1. Selection Statement
  2. Iteration Statement
  3. Jump Statement

Selection Statement

It allows us to pick one possible path of execution out of many possible paths, based on a condition known only at runtime. It has two variants with comparable use cases:

Here is an example on how if-statement works. You can visit above links to know more about them in detail.

The output is :

 


Iteration Statement

It allows us to run a block of code again and again based on some condition. It is also called a loop. It has following four variants :

Here is an example on how while loop works. You can visit above links to know more about them in detail.

The output is:

 


Jump Statement

It allows us to skip running some portion of code and continue execution at some other location in the program. It has following three variants:

Here is an example on how break-statement works. You can visit above links to know more about them in detail.

The output is :