Loop control structures in Java

We see for, while, and do while loops, along with other programming structures such as break, continue, and return.

Loops are used to execute a set of instructions multiple times, always based on a condition that will decide whether or not to keep looping. Let’s see what types there are.

while-loop

while (expression) {statements}

Statements inside braces are executed as long as the expression is true.

i=5;

while ( i > 0 ) { i –;}

// braces here could have been omitted, since

// that there is only one statement.

System.out.println(“Now i is 0”);

For Loop

It is a more “fixed” loop, it allows executing the set of statements a certain number of times set at the beginning of the loop and therefore works as a counter. Its general expression would be as follows:

for (initialization, BooleanExpression, increment) {setOfStatements;}

for (int i= 0; i <10; i++){ System.out.println("the value of i is: " + i); }

This example would show us ten lines on the screen telling us the increasing value of ‘i’ from zero to nine.

do-while loop

It is the same as the while loop seen above, only now the expression at the end of the loop is evaluated, so that set of statements is executed at least once:

i=5;

do

{i –;} // braces here can be omitted since

while ( i > 0 ) // that there is only one statement.

This similar example to the previous one for the while loop differs in that it executes the statements in its body once more since it checks the condition later.

Break, Continue, and Return Statements

See also  Cursors in SQL

Earlier we talked about the Break statement with switch branches. Well, this sentence has a broader value. The break statement allows us to get out of the block of statements (enclosed in braces) or the loop that we are executing, without executing the remaining statements to the end or the remaining iterations of the loop. For example:

i=5;

do{

Yo –;

if (i == 3) break;

} while ( i > 0 )

// In this example when i has the value 3

// the loop will be exited.

The Continue statement is only valid for loops, it is used to avoid executing the remaining statements for the completion of an iteration of that loop, continuing afterwards with the following iterations of the loop. For example:

i=5;

do{

if (i == 3) continue; Yo –;

}while ( i > 0 ) // In this example when i has the value 3

// the iteration will be abandoned and therefore the

// loop will never end since it is not

// would execute the decrement statement.

Both the continue and break statements can be used with labels to be able to discriminate the loops that you want to affect in case they are in a nested loop. For example:

loop1:

for (int i=0; i loop2:

for (int j=0; i if (j==5) { break loop2;}

}

} // when j reaches 5 loop2 stops

// run until next iteration

// from block1

Finally we see the return statement. This statement also allows us to end a set of statements, with the peculiarity this time that it also ends the method or function in which it is found. In the event that we want to return a value from that function or method, we must put it after return. For example:

See also  Importance of documentation

void function example(){

int i=0;

while (i i++; //by calling it the value 100 as you have

} //could check

return i;

}

Try–Catch–Finally Block

It is a control statement related to exception handling, it has nothing to do with the loop control structures, seen in this article, except that it is also a control structure. We discuss it below, although it will be seen with examples later.

Exception handling is used to catch errors when our programs run and treat them the way we want them to. Errors caught by an exception handling system do not crash the program, and exception handling allows us to do things when those errors occur.

For now it is only good that you know that they exist, we will see them at another time.

Loading Facebook Comments ...
Loading Disqus Comments ...