Open main menu

Wikibooks β

C Programming/Statements

< C Programming

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.

printf ("Hi there!");
printf ("Hi there!");
printf ("Strange things are afoot...");

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


Labeled StatementsEdit

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 goto statement.

Within switch statements, case and default labeled statements exist. A statement of the form

case constant-expression : statement

indicates that control will pass to this statement if the value of the control expression of the switch statement matches the value of the constant-expression. A statement of the form

default : statement

indicates that control will pass to this statement if the control expression of the switch statement does not match any of the constant-expressions within the switch statement. If the default statement is omitted, the control will pass to the statement following the switch statement.

Compound StatementsEdit

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 StatementsEdit

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 printf function calls are expressions, so the statements above are expression statements.

Selection StatementsEdit

Three types of selection statements exist in C:

if ( expression ) statement

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

if ( expression ) statement else 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 else matches up with the closest unmatched if, so that the following two snippets of code are not equal:

if (expression)
  if (secondexpression) statement1;
if (expression)
    if (secondexpression) statement1;

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

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

switch ( expression ) statement

The statement here is usually compound and it contains case-labeled statements and optionally a default-labeled statement.

Iteration StatementsEdit

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

while ( 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

do statement while ( 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

for ( 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

 for (e1; e2; e3)

is the rough equivalent of

   while (e2)

except for the behavior of continue statements within s.

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

Jump StatementsEdit

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

goto identifier ;

This statement transfers control flow to the statement labeled within the given identifier.

The second, the break statement, with the form

break ;

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

The third, the continue statement, with the form

continue ;

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

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

return expressionopt ;

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