Interview Questions, Answers and Tutorials

09.3 Control Flow

09.3 Control Flow

9.3 | Transfer Statement

Java provides six language constructs to transfer control or exit loops in a program.


  1. break
  2. continue
  3. return
  4. try …catch …finally
  5. throw
  6. assert
 
break …statement
 
break …statement terminates a loop.
 
For example, the following code showed how to terminate a while and for a loop.
 
Break Statement by testinganswers.com



continue …statement


continue …statement exits the current iteration and starts executing the next iteration.


For example, the following code prints all numbers except number 3.



return …statement

return …statement stops code execution of a method and transfer control back to the calling code.

return

try …catch …finally …statement

try …catch …finally …statement is used for handling exceptions. (Further, discuss in section ‘Exception Handling’.)


throws …statement

throws …statement is used for handling exceptions. (Further, discuss in section ‘Exception Handling’.)


assert …statement

assert …statements are used to validate the assumptions made about the program. Assertions are expected to be true when assert statements are executed. In case it is false, the Java Virtual Machine (JVM)  throws a special error of AssertionError class. Assertion errors are not handled but allowed to propagate to the top level.

Note: Assertion facility can be enabled or disabled at run-time. If disabled, assert statements are not executed during run-time.

2 thoughts on “09.3 Control Flow

Comments are closed.