Jump to content

C Programming/Statements

From Wikibooks, open books for an open world
Previous: Code library C Programming Next: C Language Reference

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

[edit | edit source]

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. (In this case, the type of the constant-expression must be an integer or character.)

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. Within a switch statement, there can be only one default statement, unless the switch statement is within another switch statement.

Compound Statements

[edit | edit source]

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

[edit | edit source]

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 statements such as printf ("Hello World!\n"); are expression statements.

Selection Statements

[edit | edit source]

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;
 else
   statement2;

 if (expression)
   {
     if (secondexpression) statement1;
   }
 else
   statement2;

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

[edit | edit source]

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)
    s;

is the rough equivalent of

  {
    e1;
    while (e2)
      {
        s;
        e3;
      }
  }

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 Statements

[edit | edit source]

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 with the given identifier. The statement must be within the same function as the goto.

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 iteration statements to transfer control flow to the place just before the end of the substatement. In for statements the iteration expression (e3 above) will then be executed before the controlling expression (e2 above) 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.

Previous: Code library C Programming Next: C Language Reference