# Knowledge Base

 for

Starts a for loop to allow some operations to be repeated a number of times. A while loop can be implemented when using the three argument version of a for loop.

 Supported Product: FDTD, MODE, DEVICE, INTERCONNECT

 Syntax Description for(x=1:100) { ?x; } Single argument for loop. The loop will be sequentially executed for each value of x. for(x=1; x<= 100; x=x+1) { ?x; } Three argument for loop. x=1 at the start of the loop. The loop continues while x <=100 and sets x=x+1 at each pass. x=1; for(0; x<10; 0) {  ?x;  x=x+1; } This is equivalent to a while loop that will execute while x<10.

Examples

This example shows a simple for loop where x takes the values 1, 3, 5, 7, 9.

a=1:2:10;

for(x=a) {

?x;

Nested loops: This example shows a nested for loop.

for(i=1:100) {

for(j=1:100) {

x(i,j) = i^2+j;

?x;

}

While loops: There is no "while" command in the scripting language, but the "for" command can be used to implement a "while" command. The command for(0; conditional_expression; 0) {} is the same as while(conditional_expression) {}. The β0β statements in the βforβ loop do nothing and are just placeholders because the scripting language expects an argument there.

# implementation of a while loop in languages that support while loops

x=1;

while(x<10) {

?x;

x=x+1;

}

# equivalent implementation of a while loop in Lumerical script language

x=1;

for(0; x<10; 0) {

?x;

x=x+1;

}