|
A program statement is any valid expression (usually an assignment or a function call) that is immediately followed by a semicolon, or it is one of the special statements described in the following.A label can optionally precede any statement and consists of an identifier followed immediately by a colon (see the goto statement). Compound StatementsProgram statements contained within a pair of braces are known collectively as a compound statement or block and can appear anywhere in the program that a single statement is permitted.A block can have its own set of variable declarations, which override any similarly named variables defined outside the block.The scope of such local variables is the block in which they are defined. The break StatementGeneral Format: break; Execution of a break statement from within a for, while, do, or switch statement causes execution of that statement to be immediately terminated. Execution continues with the statement that immediately follows the loop or switch.
The continue StatementGeneral Format: continue; Execution of the continue statement from within a loop causes any statements that follow the continue in the loop to be skipped. Execution of the loop otherwise continues as normal. The do StatementGeneral Format: do programStatement while ( expression ); programStatement is executed as long as expression evaluates to nonzero. Note that, because expression is evaluated each time after the execution of programStatement, it is guaranteed that programStatement will be executed at least once. The for StatementFormat 1: for ( expression_1; expression_2; expression_3 ) programStatement expression_1 is evaluated once when execution of the loop begins. Next, expression_2 is evaluated. If its value is nonzero,programStatement is executed and then expression_3 is evaluated. Execution of programStatement and the subsequent evaluation of expression_3 continue as long as the value of expression_2 is nonzero. Because expression_2 is evaluated each time before programStatement is executed, programStatement might never be executed if the value of expression_2 is 0 when the loop is first entered. Variables local to the for loop can be declared in expression_1.The scope of such variables is the scope of the for loop. For example for ( int i = 0; i < 100; ++i) ... declares the integer variable i and sets its initial value to 0 when the loop begins.The variable can be accessed by any statements inside the loop, but it is not accessible after the loop is terminated.
Format 2: for ( var in expression ) programStatement This variant of the for loop sets up a fast enumeration. var is a variable whose type can also be declared, making its scope local to the for loop. expression is an expression that produces a result that conforms to the NSFastEnumeration protocol. Typically, expression is a collection, such as an array or a dictionary. Each time through the for loop, the next object produced by the initial evaluation of expression is assigned to var and the body of the loop, represented by programStatenent, is executed. Execution terminates when all objects in expression have been enumerated. Note that the for loop cannot change the contents of the collection. If it does, an exception is raised. An array has each of its elements enumerated in order. Enumerating a dictionary object results in each key being enumerated, in no particular order. Enumeration of a set results in each member of the set being enumerated, in no particular order. The goto StatementGeneral Format: goto identifier; Execution of the goto causes control to be sent directly to the statement labeled identifier. The labeled statement must be located in the same function or method as the goto. The if StatementFormat 1: if ( expression ) programStatement If the result of evaluating expression is nonzero, programStatement is executed; otherwise, it is skipped. Format 2: if ( expression ) programStatement_1 else programStatement_2 If the value of expression is nonzero,programStatement_1 is executed; otherwise, programStatement_2 is executed. If programStatement_2 is another if statement, an if-else if chain is affected, like so: if ( expression_1 ) programStatement_1 else if ( expression_2 ) programStatement_2 ... else programStatement_n An else clause is always associated with the last if statement that does not contain an else. Braces can be used to change this association if necessary. The null StatementGeneral Format: ; Execution of a null statement has no effect and is used primarily to satisfy the requirement of a program statement in a for, do, or while loop.The following statement copies a character string pointed to by from to one pointed to by to: while ( *to++ = *from++ ) ; In this statement, the null statement is used to satisfy the requirement that a program statement appear after the looping expression of the while. The return StatementFormat 1: return; Execution of the return statement causes program execution to be immediately returne to the calling function or method. This format can be used only to return from a function or method that does not return a value. If execution proceeds to the end of a function or method and a return statement is not encountered, it returns as if a return statement of this form had been executed. Therefore, in such a case, no value is returned. Format 2: return expression; The value of expression is returned to the calling function or method. If the type of expression does not agree with the return type declared in the function or method declaration, its value is automatically converted to the declared type before it is returned. The switch StatementGeneral Format: switch ( expression ) { case constant_1: programStatement programStatement ... break; case constant_2: programStatement programStatement ... break; ... case constant_n: programStatement programStatement ... break; default: programStatement programStatement ... break; } expression is evaluated and compared against the constant expression values constant_1,constant_2, ...,constant_n. If the value of expression matches one of these case values, the program statements that immediately follow are executed. If no case value matches the value of expression, the default case, if included, is executed. If the default case is not included, no statements contained in the switch are executed. The result of the evaluation of expression must be of integral type, and no two cases can have the same value. Omitting the break statement from a particular case causes execution to continue into the next case. The while StatementGeneral Format: while ( expression ) programStatement programStatement is executed as long as the value of expression is nonzero. Because expression is evaluated each time before the execution of programStatement, programStatement might never be executed. |