📄 m2_op_increment.hlp
字号:
{smcl}
{* 18mar2005}{...}
{cmd:help m2 op_increment}
{hline}
{* index operators}{...}
{* index increment operator}{...}
{* index decrement operator}{...}
{title:Title}
{p 4 4 2}
{hi:[M-2] op_increment -- Increment and decrement operators}
{title:Syntax}
{cmd:++}{it:i} increment before
{cmd:--}{it:i} decrement before
{it:i}{cmd:++} increment after
{it:i}{cmd:--} decrement after
{p 4 4 2}
where {it:i} must be a real scalar.
{title:Description}
{p 4 4 2}
{cmd:++}{it:i} and {it:i}{cmd:++}
increment {it:i}; they perform the operation {it:i}={it:i}+1.
{cmd:++}{it:i} performs the operation before the expression in which
it appears is evaluated, whereas {it:i}{cmd:++} performs the operation
afterward.
{p 4 4 2}
{cmd:--}{it:i} and {it:i}{cmd:--}
decrement {it:i}; they perform the operation {it:i}={it:i}-1.
{cmd:--}{it:i} performs the operation before, and {it:i}{cmd:--} performs the
operation after the evaluation of the expression in which they appear.
{title:Remarks}
{p 4 4 2}
These operators are used in code, such as
{cmd:x[i++] = 2}
{cmd:x[--i] = 3}
{cmd:for (i=0; i<100; i++) {c -(}}
...
{cmd:{c )-}}
{cmd:if (++n > 10) {c -(}}
...
{cmd:{c )-}}
{p 4 4 2}
Where these expressions appear, results are as if the current value of
{cmd:i} were substituted, and in addition, {cmd:i} is incremented, either
before or after the expression is evaluated. For instance,
{cmd:x[i++] = 2}
{p 4 4 2} is equivalent to
{cmd:x[i] = 2} {cmd:;} {cmd:i = i + 1}
{p 4 4 2}
and
{cmd:x[++i] = 3}
{p 4 4 2} is equivalent to
{cmd:i = i + 1} {cmd:;} {cmd:x[i] = 3}
{p 4 4 2}
Coding
{cmd:for (i=0; i<100; i++) {c -(}}
...
{cmd:{c )-}}
{p 4 4 2}
or
{cmd:for (i=0; i<100; ++i) {c -(}}
...
{cmd:{c )-}}
{p 4 4 2} is equivalent to
{cmd:for (i=0; i<100; i=i+1) {c -(}}
...
{cmd:{c )-}}
{p 4 4 2}
because it does not matter whether the incrementation is performed
before or after the otherwise null expression.
{cmd:if (++n > 10) {c -(}}
...
{cmd:{c )-}}
{p 4 4 2} is equivalent to
{cmd:n = n + 1}
{cmd:if (n > 10) {c -(}}
...
{cmd:{c )-}}
{p 4 4 2}
whereas
{cmd:if (n++ > 10) {c -(}}
...
{cmd:{c )-}}
{p 4 4 2}
is equivalent to
{cmd:if (n > 10) {c -(}}
{cmd:n = n + 1}
...
{cmd:{c )-}}
{cmd:else n = n + 1}
{p 4 4 2}
The {cmd:++} and {cmd:--} operators may only be used with real scalars
and are usually associated with indexing or counting. They result in
fast and readable code.
{title:Conformability}
{cmd:++}{it:i}, {cmd:--}{it:i}, {it:i}{cmd:++}, and {it:i}{cmd:--}
{it:i}: 1 {it:x} 1
{it:result}: 1 {it:x} 1
{title:Diagnostics}
{p 4 4 2}
{cmd:++} and {cmd:--} are allowed with real scalars only. That is,
{cmd:++i} or {cmd:i++} is valid, assuming {cmd:i} is a real scalar, but
{cmd:x[i,j]++} is not valid.
{p 4 4 2}
{cmd:++} and {cmd:--} abort with error if applied to a variable that is not
a real scalar.
{p 4 4 2}
In addition, evaluation of the suffix {cmd:++} or {cmd:--} is postponed until
the entire expression has been evaluated, thus,
{cmd:x[i++] = y[i]}
{p 4 4 2}
is equivalent to
{cmd:x[i] = y[i]}
{cmd:i++}
{p 4 4 2}
Coding {cmd:x[i++] = y[i]} is acceptable, but coding
{cmd:x[i] = y[i++]}
{p 4 4 2}
is better because it emphasizes that {cmd:i} is incremented
afterward, and many programmers feel that {cmd:i++}
or {cmd:++i} should never be coded in a line that has a second
reference to {cmd:i}, before or after.
{p 4 4 2}
Coding {cmd:x[++i] = y[i]} and
coding {cmd:x[i] = y[++i]} have the same
effect, for similar reasons.
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-2] op_increment}
{p 4 13 2}
Online: help for
{bf:{help m2_exp:[M-2] exp}};
{bf:{help m2_intro:[M-2] intro}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -