Mitsubishi MELFA CR1D Instruction Manual page 213

Table of Contents

Advertisement

For - Next (For-next)
[Function]
Repeatedly executes the program between the For statement and Next statement until the end conditions
are satisfied.
[Format]
For[]<Counter> = <Default value> To <End Value> [Step <Increment>]
:
Next[] [<Counter 1>]
[Terminology]
<Counter>
<Default Value>
<End Value>
<Increment>
[Reference Program]
(1) A program that adds the numbers 1 to 10
1 MSUM=0
2 For M1=1 TO 10
3 MSUM=MSUM+M1
4 Next M1
(2) A program that puts the result of a product of two numbers into a 2-dimensional array variable
1 Dim MBOX(10,10)
2 For M1=1 To 10 Steo 1
3 For M2=1 To 10 Step 1
4 MBOX(M1,M2)=M1*M2
5 Next M2
6 Next M1
[Explanation]
(1) It is possible to describe For-Next statements between other For-Next statements.Jumps in the program
caused by the For-Next instruction will add one more level to the control structure in a program. It is
possible to make the control structure of a program up to 16 levels deep. An error occurs at execution if
16 levels are exceeded.
(2) If a GoTo instruction forces the program to jump out from between a For statement and a Next state-
ment, the free memory available for control structure (stack memory) decreases. Thus, if a program is
executed continuously, an error will eventually occur. Write a program in such a way that the loop exits
when the condition of the For statement is met.
(3) A run-time error occurs under the following conditions.
*The counter's <Default Value> is greater than <End Value> and <Increment> is a positive number.
*The counter's <Default Value> is smaller than <End Value>, and <Increment> is a negative number.
(4) A run-time error occurs if a For statement and a Next statement are not paired.
(5) When the Next statement corresponds to the closest For statement, the variable name in the Next state-
ment can be omitted. In the example, "M2" in step 5 and "M1" in step 6 can be omitted. The processing
speed will be slightly faster to omit the counter variable.
Describe the numerical variable that represents the counter for the number of repetitions.
Same for <Counter 1> and <Counter 2>.
Set default value of the counter for the number of repetitions as a numeric operation
expression.
Set the end value of the counter for the number of repeats as a numeric operation
expression.
Set the value of the increments for the counter for the number of repetitions as a numeric
operation expression. It is allowed to omit this argument, including STEP.
' Initialize the total MSUM.
' Increase the counter by 1 from 1 to 10 for the numeric variable M1.
' Add M1 value to numeric variable MSUM.
' Return to step 2.
' Reserve space for a 10Å~10 array.
' Increase the counter by 1 from 1 to 10 for the numeric variable M1.
' Increase the counter by 1 from 1 to 10 for the numeric variable M2.
' Substitute the value of M1*M2 for the array variable MBOX (M1, M2).
' Return to step 3.
' Return to step 2.
Detailed explanation of command words 4-200
4MELFA-BASIC IV

Advertisement

Table of Contents
loading

This manual is also suitable for:

Melfa cr2dMelfa cr3d

Table of Contents