Statements

1.7 Statements

A statement specifies an action to be performed, such as an arithmetic operation or a function call. Many statements serve to control the flow of a program by defining loops and branches. Statements are processed one after another in sequence, except where such control statements result in jumps.

Every statement that is not a block is terminated by a semicolon.

1.7.1 Block and Expression Statements

A block , also called a compound statement, groups a number of statements together into one statement. A block can also contain declarations.

The syntax for a block is:

{[list of declarations][list of statements]}

Here is an example of a block:

{ int i = 0;  /* Declarations  */
  static long a;
  extern long max;
 
  ++a;  /* Statements  */
  if( a >= max)
  {  . . .  }  /* A nested block */
  . . .
}

The declarations in a block normally precede the statements. However, ANSI C99 permits free placement of declarations.

New blocks can occur anywhere within a function block. Usually a block is formed wherever the syntax calls for a statement, but the program requires several statements. This is the case, for example, when more than one statement is to be repeated in a loop.

An expression statement is an expression followed by a semicolon. The syntax is:

[expression] ;

Here is an example of an expression statement:

 y = x;  // Assignment

The expression an assignment or function call, for example is evaluated for its side effects. The type and value of the expression are discarded.

A statement consisting only of a semicolon is called an empty statement, and does not peform any operation. For example:

for ( i = 0;  str[i] != '\0'; ++i )
  ;  // Empty statement

1.7.2 Jumps

The following statements can be used to control the program flow:

         Selection statements: if ... else or switch

         Loops: while, do ... while or for

         Unconditional jumps: goto, continue, break or return

if ... else

 


 

 

The if statement creates a conditional jump.

Syntax:

if (expression) statement1  [else  statement2]

The expression must have a scalar type. First, the if statement's controlling expression is evaluated. If the result is not equal to 0 in other words, if the expression yields "true" then statement1 is executed. Otherwise, if else is present, statement2 is executed.

Example:

if (x > y)  max = x; // Assign the greater of x and y to 
else  max = y; // the variable max.

The use of else is optional. If the value of the controlling expression is 0, or "false", and else is omitted, then the program execution continues with the next statement.

If several if statements are nested, then an else clause always belongs to the last if (in the given block nesting level) that does not yet have an else clause. An else can be assigned to a different if by creating explicit blocks.

Example:

if ( n > 0 
{  if ( n % 2 == 0 
 puts("n is positive and even");
}
else  // Belongs to first if 
 puts("n is negative or zero");

switch

 


 

 

In a switch statement, the value of the switch expression is compared to the constants associated with case labels. If the expression evaluates to the constant associated with a case label, program execution continues at the matching label. If no matching label is present, program execution branches to the default label if present; otherwise execution continues with the statement following the switch statement.

Syntax:

switch ( expression ) statement

The expression is an integer expression and statement is a block statement with case labels and at most one default label. Every case label has the form caseconst:, where const is a constant integer expression. All case constants must be different from one another.

Example:

switch( command )  // Query a command obtained
{  // by user input in a menu, 
 // for example.
  case 'a':
  case 'A': action1();  // Carry out action 1,
  break;  // then quit the switch.
  case 'b':
  case 'B': action2();  // Carry out action 2,
  break;  // then quit the switch.
  default:  putchar('\a'); // On any other "command":
   // alert.
}

After the jump from the switch to a label, program execution continues sequentially, regardless of other labels. The break statement can be used to exit the switch block at any time. A break is thus necessary if the statements following other case labels are not to be executed.

Integer promotion is applied to the switch expression. The case constants are then converted to the resulting type of the switch expression.

1.7.3 Loops

A loop consists of a statement or block, called the loop body, that is executed several times, depending on a given condition. C offers three statements to construct loops: while, do ... while, and for.

In each of these loop statements, the number of loop iterations performed is determined by a controlling expression. This is an expression of a scalar type, i. e., an arithmetic expression or a pointer. The expression is interpreted as "true" if its value is not equal to 0; otherwise it is considered "false".

Syntactically, the loop body consists of one statement. If several statements are required, they are grouped in a block.

while

 


 

 

The while statement is a "top-driven" loop: first the loop condition (i. e., the controlling expression) is evaluated. If it yields "true", the loop body is executed, and then the controlling expression is evaluated again. If it is false, program execution continues with the statement following the loop body.

Syntax:

while ( expression )  statement

Example:

s = str;  // Let the char pointer s  
while( *s != '\0') // point to the end of str 
++s; 

do ... while

 


 

 

The do ... while statement is a "bottom-driven" loop: first the body of the loop is executed, then the controlling expression is evaluated. This is repeated until the controlling expression is "false", or 0.

The key difference from a while statement is that a do ... while loop body is always executed at least once. A while loop may not execute at all, because its expression could be false to begin with.

Syntax:

do statement  while ( expression ) ;

Example:

i = 0;
do  // Copy the string str1
str2[i] = str1[i];  // to string str2 
while ( str1[i++] != '\0' ); 

for

 


 

 

A typical for loop uses a control variable and performs the following actions on it:

1.       Initialization (once before beginning the loop)

2.       Tests the controlling expression

3.       Makes adjustments (such as incrementation) at the end of each loop iteration

The three expressions in the head of the for loop define these three actions.

Syntax:

for ([expression1]; [expression2]; [expression3]
   statement

expression1 and expression3 can be any expressions. Expression2 is the controlling expression, and hence must have a scalar type. Any of these expressions can be omitted. If expression2 is omitted, the loop body is executed unconditionally. In ANSI C99, expression1 may also be a declaration. The scope of the variable declared is then limited to the for loop.

Example:

for (int i = DELAY; i > 0; --i)  // Wait a little
  ;

Except for the scope of the variable i, this for loop is equivalent to the following while loop:

int i = DELAY; // Initialize 
while( i > 0)  // Test the controlling expression
--i; // Adjust

1.7.4 Unconditional Jumps

goto

 


 

 

The goto statement jumps to any point within a function. The destination of the jump is specified by the name of a label.

Syntax:

goto label_name;

A label is a name followed by a colon that appears before any statement.

Example:

for ( ... )  // Jump out of
  for ( ... )  // nested loops. 
 if ( error 
 goto  handle_error;
  ...
 handle_error:  // Error handling here
 ...

The only restriction is that the goto statement and the label must be contained in the same function. Nonetheless, the goto statement should never be used to jump into a block from outside it.

continue

 


 

 

The continue statement can only be used within the body of a loop. It jumps over the remainder of the loop body. Thus in a while or do ... while loop, it jumps to the next test of the controlling expression, and in a for loop it jumps to the evaluation of the per-iteration adjustment expression.

Syntax:

continue;

Example:

for (i = -10; i < 10; ++i
{  ...
  if (i == 0) continue;  // Skip the value 0
...
}

break

 


 

 

The break statement jumps immediately to the statement after the end of a loop or switch statement. This provides a way to end execution of a loop at any point in the loop body.

Syntax:

break;  

Example:

while (1
{  ...
  if (command == ESC) break;  // Exit the loop 
...  
}

return

 


 

 

The return statement ends the execution of the current function and returns control to the caller. The value of the expression in the return statement is returned to the caller as the return value of the function.

Syntax:

return expression;

Example:

int max( int a, int b )  // The maximum of a and b
{ return (a>b ? a : b); }

Any number of return statements can appear in a function.

The value of the return expression is converted to the type of the function if necessary.

The expression in the return statement can be omitted. This only makes sense in functions of type void, however in which case the entire return statement can also be omitted. Then the function returns control to the caller at the end of the function block.

 



C Pocket Reference
C Pocket Reference
ISBN: 0596004362
EAN: 2147483647
Year: 2002
Pages: 29

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net