C Programming/Statements

A statement is a command given to the computer that instructs the computer to take a specific action, such as display to the screen, or collect input. A computer program is made up of a series of statements.

In C, a statement can be any of the following:

Labeled Statements
A statement can be preceded by a label. Three types of labels exist in C.

A simple identifier followed by a colon is a label. Usually, this label is the target of a  statement.

Within  statements,   and   labeled statements exist.

A statement of the form

constant-expression  statement

indicates that control will pass to this statement if the value of the control expression of the  statement matches the value of the constant-expression. (In this case, the type of the constant-expression must be an integer or character.)

A statement of the form

statement

indicates that control will pass to this statement if the control expression of the  statement does not match any of the constant-expressions within the   statement. If the  statement is omitted, the control will pass to the statement following the   statement. Within a  statement, there can be only one   statement, unless the   statement is within another   statement.

Compound Statements
A compound statement is the way C groups multiple statements into a single statement. It consists of multiple statements and declarations within braces (i.e.  and  ). In the ANSI C Standard of 1989-1990, a compound statement contained an optional list of declarations followed by an optional list of statements; in more recent revisions of the Standard, declarations and statements can be freely interwoven through the code. The body of a function is also a compound statement by rule.

Expression Statements
An expression statement consists of an optional expression followed by a semicolon. If the expression is present, the statement may have a value. If no expression is present, the statement is often called the null statement.

The  function calls are expressions, so statements such as   are expression statements.

Selection Statements
Three types of selection statements exist in C:

expression  statement

In this type of if-statement, the sub-statement will only be executed iff the expression is non-zero.

expression  statement   statement

In this type of if-statement, the first sub-statement will only be executed iff the expression is non-zero; otherwise, the second sub-statement will be executed. Each  matches up with the closest unmatched , so that the following two snippets of code are not equal:

because in the first, the  statement matches up with the if statement that has   for a control, but in the second, the braces force the   to match up with the if that has   for a control.

Switch statements are also a type of selection statement. They have the format

expression  statement

The expression here is an integer or a character. The statement here is usually compound and it contains case-labeled statements and optionally a default-labeled statement. The compound statement should not have local variables as the jump to an internal label may skip over the initialization of such variables.

Iteration Statements
C has three kinds of iteration statements. The first is a while-statement with the form

expression  statement

The substatement of a while runs repeatedly as long as the control expression evaluates to non-zero at the beginning of each iteration. If the control expression evaluates to zero the first time through, the substatement may not run at all.

The second is a do-while statement of the form

statement    expression

This is similar to a while loop, except that the controlling expression is evaluated at the end of the loop instead of the beginning and consequently the sub-statement must execute at least once.

The third type of iteration statement is the for-statement. In ANSI C 1989, it has the form

expressionopt  expressionopt   expressionopt   statement

In more recent versions of the C standard, a declaration can substitute for the first expression. The opt subscript indicates that the expression is optional.

The statement

is the rough equivalent of

except for the behavior of  statements within.

The  expression represents an initial condition;   a control expression; and   what to happen on each iteration of the loop. If  is missing, the expression is considered to be non-zero on every iteration, and only a   statement within   (or a call to a non-returning function such as   or  ) will end the loop.

Jump Statements
C has four types of jump statements. The first, the  statement, is used sparingly and has the form

identifier

This statement transfers control flow to the statement labeled with the given identifier. The statement must be within the same function as the.

The second, the break statement, with the form

is used within iteration statements and  statements to pass control flow to the statement following the while, do-while, for, or switch.

The third, the continue statement, with the form

is used within the substatement of iteration statements to transfer control flow to the place just before the end of the substatement. In  statements the iteration expression (  above) will then be executed before the controlling expression (  above) is evaluated.

The fourth type of jump statement is the  statement with the form

expressionopt

This statement returns from the function. If the function return type is, the function may not return a value; otherwise, the expression represents the value to be returned.