See King Chapters 5, 6 and 7 for more details

Warning: Is your home computer a true 32-bit machine (like falun) or a 16-bit computer?
Do you know how to make your C/C++ compiler support 32-bit operations?

C Programming

C Control Structures

There are a number of control structures in C, we briefly list them here:
 

  1. Compound statement
  2. { statements }
    
    
  3. Conditional statement

  4.  
    if ( expression )
        statement
    if ( expression )          if ( expression ) {
        statement                    statements
    else                       } else {
        statement                    statements
                             }
  5. While statement

  6.  
    while ( expression )       while ( expression )
        statement              { statements }
     
    
  7. Do-while or Do-until statement

  8.  
    do
        statement
    while ( expression ) ;

    The iteration continues when the expression evaluates to a non-zero value, a zero value ends the iterating.

  9. For statement

  10.  
    for ( expression1 ; expression2 ; expression3 )
        statement;

    All three expressions are optional

      expression1 is evaluated once at the start of the loop
      expression2 is evaluated at the start of each iteration, if the value of this expression is 0 the loop is exited
      expression3 is executed at the end of each iteration.
    The for statement is equivalent to:
    expression1;
    while ( expression2 ) {
        statement
        expression3;
    }
     
  11. Switch statement

  12.  
    switch ( expression ) {
        case constant : statement
        case constant : statement
        case constant : statement
            .
            .
            .
        default : statement
    } ;

    The expression in the switch must evaluate to an integer value, all the case labels (constant) must also have integer values. When the switch statement is entered the expression is evaluated and control transfers to the matching case (like a goto statement). If there is no matching case default is used, the default case is optional. Control flows from one case to the next, the statement is not automatically exited when the next case is reached

  13. Break statement
  14. break ;

    The break statement causes the termination of the smallest enclosing while, do, for or switch statement A break is commonly used to separate the cases in a switch statement.

  15. Continue statement
  16. continue ;

    Causes control to transfer to the loop continuation (end of the loop) portion of the smallest enclosing while, do or for statement This statement is used to transfer control to the next iteration of the loop, that is, terminate the current iteration and start on the next iteration

Example: use of break

In the example we use the following strategy to read from a file or terminal
 

read first line
while ( not-at-end ) {
    process the line
    read next line
}
 

The main problem with this approach is that we need two read statements.
An alternative schema (plan) uses the break statement
 

while ( TRUE ) {
    read next line
    if ( EOF )
      break;
    process the line
}