Witscale Test Center

5.1 Flow control statements


5.1 Flow control statements

A Java program that has a list of sequential statements is simple to understand and write. The statements in such program are executed in the same order in which they appear in the program. However most real applications demand more control on the order in which the statements will be executed. There are three types of execution-

A sequential execution that starts at the top and continues to the bottom.

A conditional execution where either of two program flows is selected based on some condition.

An iterative execution where a particular set of instructions is repetitively executed ( generay based on some condition).

Java provides special statements that can dictate the order of execution. These are called the flow control statements. With these statements, you can check some conditions and decide the execution path based on the results. In addition to these conventional types of flow control, Java provides exception mechanism that helps you separate the flow when certain error conditions occur. Java’s flow control statements can be summarized as:

 

Conditional flow control statements like if, if-else and switch-case

Iterative statements such as for, while, do-while

Exception handling statements like try-catch-finally, throw

Ad hoc flow control statements like break, continue with or without label:

 

 Java’s flow control statements if-else and for, while, do-while loops are similar to procedural programming languages such as C, C++. However, Java does not support the controversial control statement goto, which is quite common to most procedural languages. Instead, you can use the break and continue statements along with labels to achieve goto-like functionality.