📄 statemen.gml
字号:
.ix 'statement' 'compound'
compound statement
or
.ix block
block)
is executed repeatedly until the controlling expression
is equal to zero.
.beglevel
.keep begin
.*
.section The while Statement
.*
.ix 'while statement'
.ix 'statement' 'while'
.cillust begin
.mono while
.mono (
expression
.mono )
statement
.cillust end
.keep end
.pp
The evaluation of the controlling expression takes place before each
execution of the loop body (:ITAL.statement:eITAL.).
If the expression evaluates to zero the first time, the loop body
is not executed at all.
.pp
The
.us statement
may be a compound statement.
.keep begin
..sk 1 c
For example,
.millust begin
char * ptr;
/* ... */
while( *ptr != '\0' ) {
if( *ptr == '.' )break;
++ptr;
}
.millust end
.keep end
.pc
The loop will scan characters pointed at by
.mono ptr
until either a null character or a dot is found.
If the initial value of
.mono ptr
points at a null character, then no part of the loop body will
be executed, leaving
.mono ptr
pointing at the null character.
.*
.section The do Statement
.*
.ix 'do statement'
.ix 'statement' 'do'
.cillust begin
.mono do
statement
.mono while
.mono (
expression
.monoon
);
.monooff
.cillust end
.pp
The evaluation of the controlling expression takes place after each
execution of the loop body (
..ct .us statement
..ct ).
If the expression evaluates to zero the first time, the loop body
is executed exactly once.
.pp
The
.us statement
may be a compound statement.
.keep begin
..sk 1 c
For example,
.millust begin
char * ptr;
char * endptr;
/* ... */
endptr = ptr + strlen( ptr );
do {
--endptr;
} while( endptr >= ptr && *endptr == ' ' );
.millust end
.keep end
.pc
In this example, the loop will terminate when
.mono endptr
finds a non-blank character starting from the right, or when
.mono endptr
goes past the beginning of the string.
If a non-blank character is found,
.mono endptr
will be left pointing at that character.
.*
.section The for Statement
.*
.pp
The statement,
.ix 'for statement'
.ix 'statement' 'for'
.cillust begin
.mono for
.mono (
expr1:MSEMI.
expr2:MSEMI.
expr3
.mono )
statement
.cillust end
.pc
is almost equivalent to,
.cillust begin
expr1:MSEMI.
.cbr
.mono while
.mono (
expr2
.mono )
.mono {
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.statement
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.expr3:MSEMI.
.cbr
.mono }
.cillust end
.pc
The
difference is that the
.ix 'continue statement'
.ix 'statement' 'continue'
.kw continue
statement will pass control to the statement
.us expr3
rather than to the end of the loop body.
.pp
.us expr1
is an initialization expression and may be omitted.
.pp
.us expr2
is the controlling expression, and specifies an evaluation to be
made before each iteration of the loop body. If the expression
evaluates to zero, the loop body is not executed, and
control is passed to the statement following the loop body.
If
.us expr2
is omitted,
then a non-zero (true) value is substituted in its place. In this
case, the statements in the loop must cause an explicit break
from the loop.
.pp
.us expr3
specifies an operation to be performed after each iteration.
A common operation would be the incrementing of a counter.
.us expr3
may be omitted.
.pp
The
.us statement
may be a compound statement.
.keep begin
..sk 1 c
For example,
.millust begin
char charvec[256];
int count;
for( count = 0; count <= 255; count++ ) {
charvec[count] = count;
}
.millust end
.keep end
.pc
This example will initialize the character array
.mono charvec
to the values from 0 to 255.
.pp
The following are examples of
.kw for
statements:
.cillust begin
.monoon
for( ;; )
.monooff
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.statement:MSEMI.
.cillust end
.pc
All statements in the body of the loop will be executed until a
.kw break
or
.kw goto
statement is executed which passes control outside of the loop, or
a
.kw return
statement is executed which exits the function.
This is sometimes called
.ix 'loop forever'
.us loop forever
..ct ..li .
.cillust begin
.monoon
for( i = 0; i <= 100; ++i )
.monooff
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.statement:MSEMI.
.cillust end
.pc
The object
.mono i
is given the initial value zero, and after each iteration of the loop
is incremented by one. The loop is executed 101 times, with
.mono i
having the successive values
.mono 0
..ct ,
.mono 1
..ct ,
.mono 2 ... 99
..ct ,
.mono 100
..ct ,
and having the value
.mono 101
after termination of the loop.
.cillust begin
.monoon
for( ; *bufptr != '\0'; ++bufptr )
.monooff
.cbr
&SYSRB.&SYSRB.&SYSRB.&SYSRB.statement:MSEMI.
.cillust end
.pc
The object
.mono bufptr
is already initialized, and the loop will continue until
.mono bufptr
points at a null character. After each iteration of the loop,
.mono bufptr
will be incremented to point at the next character.
.endlevel
.*
.section Jump Statements
.*
.pp
A jump statement causes execution to continue at
a specific place in a program, without executing any other intervening
statements.
There are four jump statements:
.kw goto
..ct ,
.kw continue
..ct ,
.kw break
and
.kw return
..ct ..li .
.beglevel
.*
.section The goto Statement
.ix 'goto statement'
.ix 'statement' 'goto'
.*
.cillust begin
.mono goto
identifier:MSEMI.
.cillust end
.pc
.us identifier
is a label somewhere in the current function (including any
block within the function).
The next statement executed will be the one following that label.
.pp
.bd Note:
it can be confusing
to use the
.kw goto
statement excessively. It is easy to create
.ix 'spaghetti code'
.us spaghetti code,
which is very difficult to understand, even by the person who wrote it.
It is recommended that the
.kw goto
statement be used, at most, to jump
.us out of
blocks, never into them.
.*
.section The continue Statement
.ix 'continue statement'
.ix 'statement' 'continue'
.*
.millust begin
continue;
.millust end
.pp
A
.kw continue
statement may only appear within a loop body, and causes a jump
to the inner-most loop's loop-continuation statement (the end of
the loop body).
.pp
In a
.ix 'continue statement' 'in a while'
.kw while
statement, the jump is effectively back to the
.kw while
..ct ..li .
.pp
In a
.ix 'continue statement' 'in a do'
.kw do
statement, the jump is effectively down to the
.kw while
..ct ..li .
.pp
In a
.ix 'continue statement' 'in a for'
.kw for
statement,
the jump is effectively to the closing brace of the compound-statement
that is the subject of the
.kw for
loop. The third expression in the
.kw for
statement, which is often an increment or decrement, is then
executed before control is returned to the top of the loop.
.*
.section The break Statement
.ix 'break statement'
.ix 'statement' 'break'
.*
.millust begin
break;
.millust end
.pp
A
.kw break
statement may only appear in an iteration (loop) body or a
.kw switch
statement.
.pp
In a loop, a
.kw break
will cause execution to continue at
the statement following the loop body.
.pp
In a
.kw switch
statement, a
.kw break
will cause execution to continue at
the statement following the
switch.
If the loop or
.kw switch
that contains the
.kw break
is enclosed inside another loop or
.kw switch
..ct ,
only the
inner-most loop or
.kw switch
is terminated.
The
.kw goto
statement may be used to terminate more than one loop or
.kw switch
..ct ..li .
.*
.section The return Statement
.ix 'return statement'
.ix 'statement' 'return'
.*
.cillust begin
.monoon
&SYSRB.&SYSRB.&SYSRB.&SYSRB.return:MSEMI.
.monooff
.cor
.monoon
&SYSRB.&SYSRB.&SYSRB.&SYSRB.return
.monooff
expression:MSEMI.
.cillust end
.pc
A popular variation of the second form is,
.cillust begin
.monoon
&SYSRB.&SYSRB.&SYSRB.&SYSRB.return(
:ITAL.expression:eITAL.
);
.monooff
.cillust end
.pc
The
.kw return
statement causes execution of the current function to be terminated,
and control is passed to the caller.
A function may contain
any number of
.kw return
statements.
.pp
If the function is declared with a return type of
.kw void
(no value is returned), then no
.kw return
statement within that function may return a value.
.pp
If the function is declared as having a return type of other than
.kw void
..ct ,
then any
.kw return
statement with an expression
will evaluate the expression and convert it to the return type.
That value will be the value returned by the function.
If a
.kw return
is executed without an expression, and the caller uses the value
returned by the function, the behavior is undefined since no value
was returned. An arbitrary value will probably be used.
.pp
Reaching the closing brace } that terminates the function is equivalent
to executing a
.kw return
statement without an expression.
.endlevel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -