BeanShellTutorial:7 Loops

Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member
Contents

Loops

Java has very flexible three looping mechanisms. You can use one of the following three loops:

• while Loop
• do...while Loop
• for Loop

The "while" loop

One other point is that the code to be executed if the conditions are met. That means that if you want to loop the next five lines with a 'while' loop, you must put a set number of spaces at the beginning of each of the next five lines.

x=0;
while( x < 20 ){
print("value of x : " + x );
x++;
}

The "for" loop

The loop based on "for" is also very popular. Let us print values from 0 to 10 in a for loop:

x=10;
for(x = 10; x < 20; x++){
print("value of x : " + x );
}

One can also break this loop if a condition is met:

x=0;
for(x=10; x < 20; x++){
if (x==13) break; '' break the loop here
print("value of x : " + x );
}

You can also use the continue statement if you want to skip some iteration, without exiting the loop:

x=10;
for(x = 10; x < 20; x++){
if (x==13) continue; '' do not print x=13
print("value of x : " + x );
}