Scripting Guide > JSL Building Blocks > Iterate Functions > Break and Continue Functions
Publication date: 07/08/2024

Break and Continue Functions

The Break() and Continue() functions give you more control over looping. Break() immediately stops the loop and proceeds to the next expression that follows the loop. Continue() is a gentler form of Break(). It immediately stops the current iteration of the loop and continues with the next iteration of the loop.

Break

Break() is typically used inside a conditional expression. For example:

For( i = 1, i <= 5, i++,
	If( i == 3, Break() );
	Print( "i=" || Char( i ) );
);

results in:

"i=1"

"i=2"

The script works like this:

For(

Begins the For() loop.

i = 1,

Sets i to 1.

i <= 5,

As long as i is less than or equal to 5, continues evaluating the loop.

i++,

Increments i by 1. Note that this step is done after the If loop is evaluated.

If(

Begins the If() loop.

i == 3, Break()

If i is equal to 3, breaks the loop.

);

Ends the loop.

Print(

When i is not equal to 3, opens the Print() loop.

"i="

Prints the string "i=" to the log.

||

Places "i=" on the same line as the value that follows.

Char(i));

Prints the value of i to the log.

The For() loop then repeats until the value of i is less than or equal to 5, breaking when i equals 3, and breaking and printing only when i is less than 3.

);

Ends the loop.

Note that when the If() and Break() expressions follow Print(), the script prints the values of i from 1 to 3, because the loop breaks after "i=3" is printed.

"i=1"

"i=2"

"i=3"

Continue

As with Break(), Continue() is typically used inside a conditional expression. For example:

For( i = 1, i <= 5, i++,
	If( i < 3, Continue() );
	Print( "i=" || Char( i ) );
);

results in:

"i=3"

"i=4"

"i=5"

The script works like this:

For(

Begins the For() loop.

i = 1,

Sets i to 1.

i <= 5,

Evaluates 1 as less than or equal to 5.

i++,

Increments i by 1. Note that this step is done after the If loop is evaluated.

If(

Begins the If() loop.

i < 3, Continue()

Evaluates i as 1 and continues as long as i is less than 3.

);

Ends the If() loop.

Print(

When i is no longer less than 3, opens the Print() loop.

"i="

Prints the string "i=" to the log.

||

Places "i=" on the same line as the value that follows.

Char(i));

Prints the value of i to the log.

The For() loop then repeats until the value of i is less than or equal to 5, continuing and printing only when i is equal to or greater than 3.

);

Ends the loop.

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).