Error: ...initial value in for loop must be numeric or character - Maple Programming Help

Home : Support : Online Help : System : Error Message Guide : initialvalueinforloop

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 The error messages related to loops indicate that elements must be numeric, character, or integer.

Examples

Initial value

 > $\mathbf{for}x\mathbf{from}\mathrm{myvariable}\mathbf{to}5\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$

If the final value is numeric, ensure that the initial value is numeric, for example, 1.

 > $\mathbf{for}x\mathbf{to}5\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 ${1}$
 ${2}$
 ${3}$
 ${4}$
 ${5}$ (2.1)
 > $\mathbf{for}x\mathbf{from}\mathrm{myvariable}\mathbf{to}"c"\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$

If the final value is a character, ensure that initial value is also a character.

 > $\mathbf{for}x\mathbf{from}"a"\mathbf{to}"c"\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 ${"a"}$
 ${"b"}$
 ${"c"}$ (2.2)

Final value

 > $y≔\mathrm{sqrt}\left(20\right)$
 ${y}{:=}{2}{}\sqrt{{5}}$ (2.3)
 > $\mathbf{for}x\mathbf{to}y\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$

Use the type command to verify whether the value is numeric or character.

 > $\mathrm{type}\left(y,'\mathrm{numeric}'\right);$
 ${\mathrm{false}}$ (2.4)
 > $\mathrm{type}\left(y,'\mathrm{character}'\right);$
 ${\mathrm{false}}$ (2.5)

Ensure that the final value is numeric or character.

 > $\mathbf{for}x\mathbf{to}\sqrt{25}\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 ${1}$
 ${2}$
 ${3}$
 ${4}$
 ${5}$ (2.6)

Increment of for loop

 > $\mathbf{for}x\mathbf{by}\sqrt{2}\mathbf{to}evalf\left(\sqrt{3}\right)\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 > $\mathrm{type}\left(\mathrm{sqrt}\left(2\right),'\mathrm{numeric}'\right)$
 ${\mathrm{false}}$ (2.7)
 > $\mathrm{type}\left(\mathrm{evalf}\left(\mathrm{sqrt}\left(2\right)\right),'\mathrm{numeric}'\right)$
 ${\mathrm{true}}$ (2.8)

Use evalf to evaluate the square root of 2, therefore making the increment numeric.

 > $\mathbf{for}x\mathbf{by}evalf\left(\sqrt{2}\right)\mathbf{to}evalf\left(\sqrt{3}\right)\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(x\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 ${1}$ (2.9)

Increment when looping over characters

 > $\mathbf{for}i\mathbf{from}"a"\mathbf{by}\frac{1}{2}\mathbf{to}"e"\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(i\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 > $\mathbf{for}i\mathbf{from}"a"\mathbf{by}2\mathbf{to}"e"\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(i\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 ${"a"}$
 ${"c"}$
 ${"e"}$ (2.10)
 > $\mathbf{for}i\mathbf{to}"g"\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(i\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 > $\mathbf{for}i\mathbf{to}7\mathbf{while}\mathrm{true}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}print\left(i\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do};$
 ${1}$
 ${2}$
 ${3}$
 ${4}$
 ${5}$
 ${6}$
 ${7}$ (2.11)