Chapter 5 statement
A C program statement controls the process of execution. In C, as in other programming languages, there are several types of statements that can be used to perform cycles, select other statements and conversion controls. Later, a brief summary statement, this chapter describes these C statements in alphabetical order:
BREAK statement if statement
Composite statement empty statement
Continue Statement Return Statement
Do-While Statement Switch Statement
Expression statement try-except statement
FOR statement try-finally statement
GOTO and label statement while statement
Site outline
The C statement consists of statement symbols, expressions, and other statements. A statement constituting the composition of another statement is referred to as "body." This chapter discusses each statement type given in the following syntax.
grammar
Statement:
Label statement
Composite statement
Expression statement
Select statement
Iterative statement
Jump statement
TRY-EXCEPT statement / * Microsoft Special Office * /
TRY_FINALLY Statement / * Microsoft Special Office * /
Statefi is frequently as a "composite statement", and a composite statement consists of other statements including keywords. The composite statement is limited by the curly bracket ({}). All other C statements end with a semicolon (;). A semicolon is a statement end.
The expression statement contains a C expression that contains the arithmetic and logical operators described in Chapter 4 "Expression and Assignment". NULL statement is an empty statement.
Any C statement can begin with a marked label, which is composed of a name of a colon. Since only the goto statement recognition statement label, the statement label is discussed with GOTO, see "GOTO and label statement" behind this chapter for more information.
BREAK statement
The Break statement is used to terminate the execution of DO, For, Switch, or While statements. Control the subsequent statement passed to the termination statement.
grammar
Jump statement:
Break;
The BREAK statement is frequently used in a Switch statement to terminate a special situation. An error is lacking when it is missing an iterative statement or a Switch statement.
In nested statements, the BREAK statement only terminates its DO, For, Switch or While statements directly.
You can use a Return or Goto statement to turn control to the outside of the nested structure.
The following example explains the BREAK statement:
For (i = 0, i This example processes an array of variable length strings stored in Lines. BREAK statement After the end of the space character ('/ 0') is found in each string, exit the FOR loop in the layer and store the position in Lengths [i]. The variable j does not increase when Break is in exit from the inner layer. Then control returns to the outer FOR loop. I increase and repeat this process until I is greater than or equal to Length. Composite statement A composite statement (also known as a "block") is generally an example of a statement that is another statement such as an IF statement. Chapter 3, "Description and Type", describe the format and meaning of the description of the beginning of a composite statement. grammar Composite statement: {Description Table OPT statement table OPT} Description Table: Description Description table description Statement table: Statement Statement table statement If you have any description, they must be placed before any statement. Each range of identifiers explained in a complex statement extends from its description point to the end. It is visible in this block unless an identifier of the same name is present in a block of layers. The identifier in a composite statement assumes that Auto unless explicitly explained in REGISTER, STATIC or Extern, except for the function, because the function can only be extern. You can omit the extern indicator in the function description, which is still extern. If a variable or function is used in a composite statement, it is not assigned, and the storage cannot be assigned, and the initialization cannot be performed. This instruction refers to an external variable or function defined elsewhere. The variables described in a block in a block are reinprinted, and if necessary, initialization is performed each time it enters the composite statement. These variables are not defined when the complex statement exits. If a variable describes and has a static property, initialize the variable at the start of the program and saves its value throughout the program. For information on Static, see "Storage" in Chapter 3, "Description and Types". This example shows a composite statement: IF (i> 0) {line [i] = x; x ; i--; In this example, if I is greater than 0, all statements in the composite statement are sequentially executed. Continue statement The Continue statement passes the control to the next iteration, bypasses the next iteration, bypasses the DO, FOR or WHILE statement of the statement. Continue is generally used to return from a deep nested loop to a loop beginning. grammar Jump statement: CONTINUE; Determine the next iteration of a Do, for or While statement as follows: * In a Do or a While statement, the next iteration is started by re-DO or WHILE statement. * In a Continue statement in a FOR statement, the first expression of the FOR statement is evaluated, then the compiler retrieves the evaluation of the expression, according to the result, termination, or iteration of the sentence. For more information on for statement and its non-finalist, see "for statement" behind this chapter. The following is an example of a Continue statement: While (i-> 0) {x = f (i); if (x == 1) Continue; y = x * x;} In this example, the statement is performed when I is greater than 0. First assign f (i) to X, then if x is equal to 1, execute the CONTINUE statement, ignore the remaining statements in the sentence, perform the top of the loop, and evaluate the test of the cycle. Do-While statement Do-While statement allows you to repeat a statement or a compound statement until a specified condition becomes false. grammar Iterative statement: DO statement while (expression); The expression in a do-While statement is evaluated after executing the cycler, so the cycler is executed at least once. This expression must be an arithmetic or pointer type. The execution process is as follows: 1. Execute a statement. 2. Next, the expression is evaluated, if the expression is false, the DO-WHILE statement terminates, controls the next statement in the steering program, if the expression is true (non-0), repeating the process, from step 1 Start. The DO-While statement is also terminated when a Break, Goto or Return statement in the sense statement is. The following is an example of a Do-While statement: DO {y = f (x); x -;} while (x> 0); In this DO-WHILE statement, Y = f (x) is performed; and X-; two statements, regardless of the initial value of X. Then, value x> 0, if x is greater than 0, then the statement body and re-evaluation x> 0, as long as X is maintained greater than 0, the statement is repeatedly executed. When the X is turned 0 or negative, the execution of the DO-WHILE statement is terminated, which is executed at least once. Expression statement When an expression statement is executed, the expression is evaluated according to the rules given in Chapter 4 "Expression and Assignment". Syntax expression statement: Expression OPT All side effects of an expression value are done before performing the next statement. An empty expression statement is called an empty statement. See "empty statement" behind this chapter for more information. These examples illustrate the expression statement: X = (Y 3); / * Assign the value of Y 3 X * / x ; / * x increase 1 * / X = y = 0 / * Put the X and Y to 0 * / Proc (arg1, arg2); / * Function call Returns void * / Y = z = (f (x) 3); / * A function call expression * / In the last function calling the expression statement, the value of the expression includes any value returned by the function to increase 3, and then assigns the variable Y and Z. For statement The FOR statement allows you to repeat the number of statements or complex statements. A FOR statement is performed 0 or more times until an optional condition becomes false. You can initialize and change the value in the FOR statement and initialize and change the value in the execution of the FOR structure. grammar Iterative statement: For (initial expression OPT; Conditional Expression OPT; Cycle Expression OPT) Statement A FOR statement is executed as follows: 1. If there is, the initial expression is evaluated, which indicates the initialization of the loop. The initial expression is not limited on the type. 2. If there is, the conditional expression must be an arithmetic or pointer type. It evaluates before each iteration, there are three possible outcomes: * If the conditional expression is true (non-0), the statement is executed. Then if there is a loop expression, it is obtained. The cyclic expression is evaluated after each iteration. Its type is not limited. Side effects will be executed in turn. Then start the process of evaluating the conditional expression. * If the conditional expression is omitted, the conditional expression is considered true, the process is exactly the same as the process described in the previous paragraph. A for statement with no conditional expression is only executed The Break or Return statement in the statement is terminated, or when an (jumping out of the FOR statement) GOTO statement is performed. * If the conditional expression is false (0), the execution termination of the FOR statement is terminated, and the control is passed to the next statement in the program. A FOR statement is also terminated when the BREAK, GOTO, or RETURN statement in the executing statement. A Continue statement in a FOR loop causes the evaluation cycle expression. The loop expression does not evaluate or execute when the BREAK statement in a FOR cycle is performed. The following statement: For (;;); It is a normal way to generate an infinite loop, which can only exit with BREAK, GOTO, or RETURN statements. The following example shows the for statement: For (i = space = Tab = 0; i This example is performed on the space ('') and tab keys ('/ t') in the character array name LINE. Count, replace each tab key with a space, first i, space and tabs are initialized to 0. Then I and constant max are compared; if i is less than MAX, the statement is executed. The statement of the IF statement is executed or not executed according to the value of line [i]. Then I increase 1 and compared with the max, the statement is repeatedly executed as long as i is smaller than the max. Goto and label statement GOTO statement conversion controls to a label. The given label must be in the same function and can only appear in front of one of the same functions. grammar Statement: Label statement Jump statement Jump statement: Goto identifier marker statement: Identifier: The statement is a statement label that is meaningful to the goto statement. In any other context, the execution of a label statement ignores the label. A jump statement must be retained in the same function, and can only appear before one statement in the same function. Following a goto identifier with its own namespace, so this name is independent of other identifiers. The label cannot be reelated. See "Name Space" in Chapter 2, "Program Structure" for more information. A good programming style is to use Break, Continue, and Return statements where you may have GOTO. A BREAK statement can only exit from a circulating layer, and a GOTO can exit from a deep nested loop. The following example shows the role of the GOTO statement: Void main () {INT I, J; For (i = 0, i <10; i ) {printf ("Outer Loop Executing. I =% D / N", I); for (j = 0; J <3 J ) {Printf ("inner loop executing. j =% d / n", j), if (i == 5) goto stop;}} / * This message does not print * / printf ("loop exited. i = % D / N ", I); STOP: Printf (" Jumped to Stop, I =% D / N ", I); In this example, a GOTO statement is switched to 5 when i is equal to 5. IF statement The IF statement control condition branch. If the value of the expression is not 0, an IF statement is executed, and the syntax of the IF statement has two formats. Syntax Selection Statement: If (Expressions) Statement If (Expression) Statement ELSE statement in both formats of the IF statement, the expression can have any type except the structure, which is evaluated, including all side effects. In the first format of the syntax, if the expression is true (non-0), the statement is executed; if the expression is false (0), the statement is ignored. In the second format of this grammar, it uses ELSE, if the value of the expression is false, the second statement is executed. Using these two formats, control all statements from the IF statement turn to the program, except one of the statements contains a Break, Continue or GOTO. The following is an example of the IF statement: IF (i> 0) y = x / i; else {x = i; y = f (x);} In this example, if I is more than 0, Y = X / I; statement; if i is less than or equal to 0, I is assigned to x, and F (x) is assigned to Y. Note that the statement forming the IF clause is at the end of a semicolon. When the IF statement and the ELSE clause, the lack is used to combine the statement. Form a more clear composite statement. If there is no in parentheses, the compiler solves the ambiguity by associating the ELSE with the nearest lack of ELSE IF. IF (i> 0) / * No curly bracket * / if (j> i) x = j; ELSE X = i; In this example, the ELSE clause is associated with the IF statement of the inner layer. If i is less than or equal to 0, no value is assigned to X. IF (i> 0) {/ * with flower brackets * / if (j> i) x = j;} else x = j; In this example, the IF statement of the mid-layer is enclosed with a flower bracket to match the ELSE clause to the outer IF statement. If i is less than or equal to 0, the I is assigned to x. Empty statement A "empty statement" is a statement containing only a semicolon. It can appear anywhere in one statement. No events do not take when an empty statement is performed. The correct way to write an empty statement is: grammar; For example, DO, For, IF and While statements require an executable statement to appear in their statement, when there is no need for a actual statement, it requires empty statements to meet grammar requirements. Just like any other C language. You can include a label before an empty statement. In order to add a reference numeral to an item that is not a statement, such as a composite statement, you can add a label to an empty statement, plug it directly into the same effect. This example illustrates the role of empty statements: For (i = 0; i <10; ling [i ] = 0) ; In this example, the loop expression of the FOR statement LINE [i ] = 0 initializes the beginning of LINE 10 elements 0, the statement as a null statement, does not have to have subsequent statements. RETURN statement RETURN statement terminates an execution of a function and returns control returns a call function. Restore execution after the calling point in the call function. A return statement also returns a value to the call function. grammar Jump syntax Return Expression OPT If an expression occurs, the value of the expression is returned to the called function; if the expression is omitted, the return value of the function is uncertain. If an expression occurs, the expression is converted into a type returned by the function. If the function is used to indicate the return type VOID, a RETURN statement containing an expression produces a warning and does not evaluate the expression. If there is no RETURN statement in a function definition, the control automatically returns to the call function after the last statement of the modified function, in which case the return value of the called function is uncertain. If you do not need to return a value, the function has a VOID return type; otherwise, the default return type is int. Many programmers enclose the expression parameters of the Return statement using parentheses. But C does not require parentheses. This example shows the role of the RETURN statement: Void Draw (INT I, Long L); Long SQ (INT S); int Main () {long y; int x; y = sq (x); DRAW (x, y); return ();} long sq INT S) {RETURN (S * S);} Void Draw (INT I, LONG L) {/ * The statement definition drawing function * / return;} In this example, the main function calls two functions: SQ and DRAW . The SQ function returns the value of x * x to Main, where the return value is gived to Y. The DRAW function illustrates a VOID function and does not return a value. Trying to assign a DRAW return value caused a diagnostic message. SWITCH statement Switch and CASE statements help control complex conditions and branch operations. The Switch statement turns the control to one statement in the statement. grammar Select statement: Switch (expression) statement label statement: Case constant expression: statement DEFAULT: statement Control the statement of the Case constant expression that shifts the value of the Switch (expression). The Switch statement can contain any number of CASE instances, but there is no two Switch constants in the same SWITCH statement with the same value. The execution of the statement begins with the selected statement and proceeds until the statement ends or until a BREAK statement shift is outside the statement. The use of the Switch statement is usually like this: Switch (expression) { Description Case constant expression If the expression is equal to the value of this constant expression, the statement is executed Break DEFAULT: If the expression is "not equal to the statement, when the constant expression" "is executed" } You can use the BREAK statement to terminate a special situation in the switch statement and jump to the end of the switch statement. Without BREAK, the execution continues to the next case, perform the statement until a BREAK or reaches the end of the statement, in some cases This continues to be hopeful. If there is no CASE constant expression equal to the value of the Switch (expression), execute the default statement. If the default statement is omitted and there is no Case match, the statement in the Switch body is not executed. You can only have a default statement, and the default statement is not necessarily placed in the final, you can place anywhere in the Switch statement. In fact, it is often more effective in the beginning of the Switch statement. A Case or Default label can only appear inside a switch statement. Switch expressions and Case constant expressions must be an integer. The value of each Case constant expression must be unique in the statement. The CASE and DEFAULT labels of the Switch statement are only used to determine the execution of the statement. Switch statement can be nested, initialize any static before performing steering any Switch statement variable. Note: Description You can appear to form a Switch body at the beginning of the composite statement, but do not perform an initialization included in the description. The Switch statement turns directly to an executable statement in the statement, bypasing a row containing initialization. The following example illustrates the role of the Switch statement: Switch (c) {CASE 'A': CAPA ; Case 'a': Lettera ; default: Total ;} All three statements of the Switch body in this example are performed when C is equal to 'a', because there is no BREAK statement before each case. Perform control steering the first statement (CAPA ), and continue to pass the statement in the statement by the statement. If C is equal to 'a', Letters and Total are incremented by 1. If C is not equal to 'A' or 'A', only Total is incremented by 1. Switch (i) {case -1: n ; break; casse 0: z ; Break; Case 1: P ; Break;} In this example, each statement is followed by each statement in the Switch body. The Break statement is forced from this statement after executing a statement. If i is equal to -1, only N increases 1, follow the BREAK statement behind the N statement causes the execution control to transfer the statement, and avoid the remaining statements. Similarly, if i is equal to 0, only z income 1; if i is equal to 1, only P is 1. The last BREAK statement does not have to be required, because the control is turned out of the body at the end of the composite statement, but for consistency includes this statement. Single statement can add multiple Case labels, as follows: Case 'a': Case 'b': Case 'C': Case 'd': Case 'E': Case 'f': HEXCVT (C); In this example, if the constant expression is equal to any letter of 'A' to 'F', then call HEXCVT function. Microsoft Special Office Microsoft C does not limit the number of CASE clauses in a Switch statement, and the number is limited only by a memory available. ANSI C Allows to 257 CASE labels in a switch statement. Microsoft C default enables Microsoft to expand, use the / zA compiler option to disable these expansion. Microsoft End TRY-EXCEPT statement Microsoft Special Office TRY-EXCEPT statement It is a C language Microsoft expansion, which enables an application to obtain program control when the event occurs, such an event is called an abnormality, and the mechanism for processing an exception is called the structure of the structure. Exceptions can be based on hardware or software-based, even when the application cannot be completely recovered from hardware or software exceptions, it may display an error message and capture the internal status of the application to help diagnose new issues. This is especially useful for intermittent problems that do not easily reproduce. grammar TRY-EXCEPT statement: __TRY composite statement __except (expression) Composite statement The composite statement after the __try statement is a protection segment. The composite statement after _ _except clause is an exception processor. The processor indicates a set of action executed when an exception occurs during the execution of the protection segment. The execution process is as follows: 1. Execute the protection segment. 2. If there is no abnormality during the execution of the protection segment, the statement after the __except clause will continue to execute the __except clause. 3. If an exception occurs during the execution of the protection segment or there is an exception in the routine of the protection segment call, the __except expression is evaluated, and its return value determines how to handle exceptions. There are three values here: Exception_Continue_Search: Exception cannot be identified, continue searching for a processor, first search for the processor containing the TRY-Except statement, then search for the next highest priority processor. Exception_Continue_execution: Anymus is identified but does not consider. Continue to execute an abnormal point. Exception_execute_handler: Exception is identified. The control steering exception processor is performed by executing the __except composite statement, and then proceeding to the abnormality point continues. Because __except expressions are evaluated as a C exception, it is limited to a single value, the conditional expression operator or comma operator. If a wider process is required, the expression can call one routine to return one of the three values listed above. Note: The exception handling of the structure works with the C and C source files. Although it cannot be specifically designed in particular, you can make sure your code has better portability by using C anomaly. The C abnormal processing mechanism also has better flexibility, which can handle any type of exception. For C programs, C abnormal processing can be used instead of exception processing of the structure. For more information, see "Unusual Processing" in Chapter 5, "Statements" in the Microsoft Visual C 6.0 Language Reference Manual. Each routine in an application has its own exception processor. __except expression is executed within the range of the __TRY statement. This means that it can access any partial variables described here. __leave keyword is valid in a try-except statement block. _ _LEVE's role is to jump to the end of the TRY-Except block. Restore execution after the end of the abnormality processor. Although a GOTO statement can be used to complete the same function, a goto statement results in the elimination of the stack. __leave statements are more efficient because it does not involve the elimination of internal operations. Exiting a try-except statement using the longjmp run function is considered to be anomalous. Jump into a __try statement is illegal, but jumps out one _ _try statement is legal. This exception processor is not called if a process is deleted in the execution of a try-except statement. example The following is an example of an exception processor and a termination processor. See the "Try-Finally Statement" in the next section of the "TRY-Finally Statement" for more information on terminating the processor. ... PUTS ("Hello"); __Try { PUTS ("in try"); __Try { PUTS ("in try"); Raise_an_exception (); } __finally { PUTS ("in finally"); } __except (PUTS ("in filter", eXception_execute_handler) { PUTS ("in except"); } PUTS ("world"); The following is the output result of the above example, add a comment on the right: Hello In try / * enters TRY * / IN try / * enters nested TRY * / Infilter / * performs a filter; therefore accepts returns 1 * / IN FINALLY / * Eliminates Nested Finally * / IN except / * transfer control to the selected processor * / World / * exit processor * / Microsoft End TRY-FINALLY statement Microsoft Special Office The TRY-FINALLY statement is a Microsoft expansion of the C statement that enables the application to ensure the cleaning (CLEANUP) code when performing a code block interrupt. Cleaning is made up of such a function, such as canceling the memory allocation to turn off the file and release the file handle. The Try-Finally statement is especially useful for such routines, and the routine has several position detections to return an error returned from the routine prematurely. grammar TRY-FINALLY statement: __TRY composite statement __finally complex statement The compound statement after __try clause is a protection segment. The compound statement after the _finally clause is to terminate the processor. The processor indicates a set of actions executed when exiting the protection segment, whether the protection segment is exited by an abnormality (abnormal termination) or by standard downlink (normal termination). Perform (downward) control through a simple order (down) to reach a __TRY statement. When the control enters the _ _try statement, it is associated with the processor to become active. The execution process is as follows: 1. Execute the protection segment. 2. Call the termination processor. 3. When the termination processor is complete, continue the statement after the __finally statement, regardless of how the protection segment is terminated (eg, except for switching the protection segment via the GOTO statement or to by a RETURN statement) before the control flow movement This termination processor is terminated. __leave keyword is valid in a try-finally statement block. _ _LEVE's role is to jump to the end of the Try-final, directly execute the termination processor, although a GOTO statement can be used to complete the same function, but a goto statement results in the elimination of the stack. __leave statements are more efficient because it does not involve the elimination of internal operations. Exiting a try-finally statement using a Return statement or a longjmp run function is considered to be normal termination. Jumping into a __try statement is illegal, but it is legal from the jump. From the start-up point to the purpose of the __final, the __finally statement must be run, which is called "local expansion". If a process is deleted when a try-finally statement is executed, the termination processor is not called. Note: The exception handling of the structure works with the C and C source files. Although it cannot be specifically designed in particular, you can make sure your code has better portability by using C anomaly. The C abnormal processing mechanism also has better flexibility, which can handle any type of exception. For C programs, C abnormal processing can be used instead of exception processing of the structure. For more information, see "Abnormal Processing" in Chapter 5, "Statements" in this book "Site" section of the Microsoft Visual C 6.0 Language Reference Manual. See how the try-finally statement works in the example of the try-finally statement. Microsoft End WHILE statement While statement allows you to repeat a statement until the specified expression becomes false. grammar Iterative statement: While (expression) statement This expression must have an arithmetic or pointer type. The execution process is as follows: 1. As a result of the expression. 2. If the expression is originally false, the While Statement is not executed. Control the next statement from the While statement steering program. If the expression is true (non-0), executing the statement and repeating this process from step 1. The While statement is terminated when a BREAK, GOTO or RETURN in this statement is executed. Use the Continue statement to terminate an iteration without exiting the While loop, the Continue statement turns the control to the next iteration of the While statement. The following is an example of a While statement: While (i> = 0) {string1 [i] = string2 [i]; i--; This example copies the characters from String2 to String1. If I is greater than or equal to 0, the string2 [i] is assigned to String [i] and I will reduce I. When I reaches 0 or less, the execution of the While statement is terminated.