Program Control Flow
This section describes the functions to control the program flow.
Control Flow #
if #
Description
If the condition is true, executes statements.
Used in FDTD and FDE.
Syntax
(1) If the expression is true, executes statements.
if expression
statements
end
or
if (expression){
statements
}
(2) If the expression is true, executes statements_1. Otherwise, executes statements_2.
if expression
statements_1
else
statements_2
end
or
if (expression){
statements_1
}
else{
statements_2
}
(3)If the expression_k is true, executes the statements_k.
if expression_1
statements_1
elseif expression_2
statements_2
...
elseif expression_k
statements_k
else
statements_k+1
end
or
if (expression_1){
statements_1
}
elseif (expression_2){
statements_2
}
...
elseif (expression_k){
statements_k
}
else{
statements_k+1
}
Example
if 0
'true-1'
elseif 0
'true-2'
elseif 1
'true-3'
else
'else-part'
end
or
if (0){
'true-1'
}
elseif (0){
'true-2'
}
elseif (1){
'true-3'
}
else{
'else-part'
}
Result:
val =
true-3
See also
while, for
while #
Description
While the expression is true, executes the statement.
Used in FDTD and FDE.
Syntax
While the expression is true, executes the statement.
while expression
statement;
end
or
while (expression){
statement;
}
Example
i = 0;
while i <= 5
printf("i = %d\n", i);
i = i + 1;
end
or
i = 0;
while (i <= 5){
printf("i = %d\n", i);
i = i + 1;
}
Result:
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
See also
if, for
for #
Description
Executes the statements for each element that meets the requirement.
Used in FDTD and FDE.
Syntax
Iterates over each element in a value set.
for element = values
statements;
end
Executes statements based on explicit initialization, condition checking, and increment expressions.
for (init; condition; increment){
statements;
}
Example
x = ["a", "sample", "string"];
for i = x
printf ("%s ", i);
end
or
x = ["a", "sample", "string"];
for (i = 1; i <= length(x); i = i + 1){
printf("%s ", x(i));
}
Result:
a sample string
See also
if, while
break #
Description
Breaks the for or while loop, the code after break sentence will not be executed.
Used in FDTD and FDE.
Syntax
Breaks the for or while loop.
break;
Example
for i = 1:100
if i == 3
break;
end
printf("i = %d\n", i);
end
Result:
i = 1
i = 2
See also
continue
continue #
Description
Terminates the current iteration and starts the next iteration of for or while loop.
Used in FDTD and FDE.
Syntax
The continue statement forces execution of the next iteration of the inner-most for or while loop to begin immediately.
continue;
Example
for i = 1:4
if i == 2
continue;
end
printf("i = %d\n", i);
end
Result:
i = 1
i = 3
i = 4
See also
break
try, catch #
Description
The try and catch statements form a control flow construct for error handling.
The try statement defines a block of code that is tested for errors during execution. If an error is detected while executing commands within the try block, the try statement is immediately exited.
The catch statement defines a block of code to be executed when an error occurs. It is optional. If a catch block is present, the program automatically executes the commands in it when an error occurs.
Both syntax forms support nesting of try and catch.
Used in FDTD and FDE.
Syntax
| Code | Function |
|---|---|
try |
Executes commands in the try block and checks errors. |
catch |
Catches the error messages from try block and processes error messages according to the catch block. |
- Execute the code block following
try. If an error occurs, thetrystatement continues in one of two ways, and finally ends withend:
- No
catchstatement: exit thetrystatement and continue executing the script afterend. - A
catchstatement exists: when an error occurs, store the error message in the variable aftercatch(e.g.,error_message), then execute the subsequent code block ofcatch(if any); if none, directly continue executing the script afterend.
try
commands;
end
...
# or
try
commands_1;
catch error_message
commands_2;
end
- Execute the code block inside
{}followingtry. If an error occurs, one of two cases applies:
- No
catchstatement: exit thetryblock and continue executing the script after the closing}. - A
catchstatement exists: when an error occurs, store the error message in the variable inside the parentheses aftercatch(e.g.,error_message), then continue executing the subsequent script.
try{
commands;
}
# or
try{
commands_1;
}
catch(error_message)
commands_2;
Example
Example 1:
try
a
catch err
D = 4;
end
err
# or
try{
a
}
catch(err)
err
D = 4;
Result:
err =
'a' is undefined function or variable
Example 2:
try
x = 10;
try
y = z;
catch inner_err
inner = inner_err;
inner
end
end
# or
try{
x = 10;
try{
y = z;
}
catch(inner_err)
inner = inner_err;
inner
}
Result:
inner =
'z' is undefined function or variable
See also
if, while
Logic and Boolean Values #
and #
Description
and is a logical operator that returns true if both operands are true; otherwise it returns false. In the software, true is represented as 1 and false as 0.
Used for all solvers.
Syntax
| Code | Function |
|---|---|
expr1 and expr2 |
Logical AND. Returns 1 if both expr1 and expr2 are true; otherwise returns 0. Equivalent to expr1 & expr2. |
Example
a = 1;
b = 2;
c = (a == 1 and b == 2);
d = (a == 0 and b == 2);
c
d
Result:
c =
1
d =
0
See also
or
or #
Description
or is a logical operator that returns true if at least one operand is true; it returns false only when both operands are false. In the software, true is represented as 1 and false as 0.
Used for all solvers.
Syntax
| Code | Function |
|---|---|
expr1 or expr2 |
Logical OR. Returns 1 if at least one of expr1 or expr2 is true; returns 0 only if both are false. Equivalent to expr1 | expr2. |
Example
a = 0;
b = 2;
c = (a == 1 or b == 2);
d = (a == 1 or b == 0);
c
d
Result:
c =
1
d =
0
See also
and
true #
Description
true is a Boolean constant representing the logical value “true”. In the software, true is numerically represented as 1.
Used for all solvers.
Syntax
| Code | Function |
|---|---|
true |
Returns the Boolean value true, which is equivalent to 1 in numeric contexts. |
Example
flag = true;
flag
Result:
flag =
1
See also
false
false #
Description
false is a Boolean constant representing the logical value “false”. In the software, false is numerically represented as 0.
Used for all solvers.
Syntax
| Code | Function |
|---|---|
false |
Returns the Boolean value false, which is equivalent to 0 in numeric contexts. |
Example
flag = false;
flag
Result:
flag =
0
See also
true

