Error: ...initial value in for loop must be numeric or character
...increment of for loop must be numeric
...increment when looping over characters must be an integer
...final value in for loop must have same type as initial
Description
Examples
The error messages related to loops indicate that elements must be numeric, character, or integer.
Initial value
forxfrommyvariableto5whiletruedoprint⁡xenddo;
Error, initial value in for loop must be numeric or character
If the final value is numeric, ensure that the initial value is numeric, for example, 1.
forxto5whiletruedoprint⁡xenddo;
1
2
3
4
5
forxfrommyvariabletocwhiletruedoprint⁡xenddo;
If the final value is a character, ensure that initial value is also a character.
forxfromatocwhiletruedoprint⁡xenddo;
a
b
c
Final value
y≔sqrt20
y:=2⁢5
forxtoywhiletruedoprint⁡xenddo;
Error, final value in for loop must be numeric or character
Use the type command to verify whether the value is numeric or character.
type⁡y,⁢'numeric';
false
type⁡y,⁢'character';
Ensure that the final value is numeric or character.
forxto25whiletruedoprint⁡xenddo;
Increment of for loop
forxby2toevalf⁡3whiletruedoprint⁡xenddo;
Error, increment of for loop must be numeric
type⁡sqrt⁡2,⁢'numeric'
type⁡evalf⁡sqrt⁡2,⁢'numeric'
true
Use evalf to evaluate the square root of 2, therefore making the increment numeric.
forxbyevalf⁡2toevalf⁡3whiletruedoprint⁡xenddo;
Increment when looping over characters
forifromaby12toewhiletruedoprint⁡ienddo;
Error, increment when looping over characters must be an integer
forifromaby2toewhiletruedoprint⁡ienddo;
e
foritogwhiletruedoprint⁡ienddo;
Error, final value in for loop must have same type as initial
forito7whiletruedoprint⁡ienddo;
6
7
See Also
evalf
for
numeric
Download Help Document