Java Loop Control - Java

What is Java Loop Control?

There may be a scenario when you need to execute a block of code several numbers of instances. In well known, statements are done sequentially: the first declaration in a function is executed first, followed by way of the second, and so on.

Programming languages offer numerous control structures that permit for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements more than one instances and following is the general form of a loop declaration in maximum of the programming languages –

loop_architecture

Java programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.

Sr.No.

Loop & Description

1

while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

2

for loop

Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

3

do...while loop

Like a while statement, except that it tests the condition at the end of the loop body.

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

Java supports the following control statements. Click the following links to check their detail.

Sr.No.

Control Statement & Description

1

break statement

Terminates thelooporswitchstatement and transfers execution to the statement immediately following the loop or switch.

2

continue statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Enhanced for loop in Java

As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse collection of elements including arrays.

Syntax

Following is the syntax of enhanced for loop −

  • Declaration − The newly declared block variable, is of a kind compatible with the elements of the array you are accessing. The variable will be available inside the for block and its value would be similar to the current array element.
  • Expression − This evaluates to the array you want to loop through. The expression may be an array variable or method call that returns an array.

Example


This will produce the following result −

What is Following?

In the following chapter, we will be learning about decision making statements in Java programming.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Topics