<http://lib.cnfolio.com/ENG421ControlStructures>
Introduction to Algorithms and Programming

Control structures



Theory of control structures in computing


Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures.


sequential structures



conditional structures



repetition structures


Source: Bohm, C., Jacopini, G. (1966). Flow diagrams, turing machines and languages with only two formation rules. Communications of the ACM, 9, 366-371.



Control structures in the C language






Decision with if statement



Pseudo code

START
   Display have a nice day
   IF raining
      THEN
         Display reminder to bring umbrella
   END IF
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int raining = 1;
  6.  
  7.    printf( "Have a nice day\n" );
  8.  
  9.    if ( raining ) /* This is the test condition */
  10.       printf( "Bring an umbrella" );
  11. }


Be careful to avoid division by zero in test conditions.

When possible, use integer values in test conditions.
Flowchart




Decision with if statement



Pseudo code

START
   Obtain input number
   IF multiple of 5
      THEN
         Display message that multiple is found
   END IF
   Display remainder of division by 5
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int number;
  6.  
  7.    scanf( "%d", &number );
  8.  
  9.    if ( ( number % 5 ) == 0 )
  10.    {
  11.       printf( "Number is multiple of 5\n" );
  12.    }
  13.  
  14.    printf( "The remainder is %d", number % 5 );
  15. }


Be careful to avoid division by zero in test conditions.

When possible, use integer values in test conditions.
Flowchart




Decision with if / else statement



Pseudo code

START
   Obtain 2 cards
   IF sum of cards equal to 21
      THEN
         Display blackjack message
      ELSE
         Display sum of cards
   END IF
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card1, card2, sum;
  6.    scanf( "%d %d", &card1, &card2 );
  7.    sum = card1 + card2;
  8.  
  9.    if ( sum == 21 )
  10.    {
  11.       printf( "Blackjack!" );
  12.    }
  13.    else
  14.    {
  15.       printf( "Sum is %d", sum );
  16.    }
  17. }


Be careful to avoid division by zero in test conditions.

When possible, use integer values in test conditions.
Flowchart




Decision with if / else statement and logical negation



Pseudo code

START
   Obtain 2 cards
   IF sum of cards not equal to 21
      THEN
         Display sum of cards
      ELSE
         Display blackjack message
   END IF
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card1, card2, sum;
  6.    scanf( "%d %d", &card1, &card2 );
  7.    sum = card1 + card2;
  8.  
  9.    if ( sum != 21 )
  10.    {
  11.       printf( "Sum is %d", sum );
  12.    }
  13.    else
  14.    {
  15.       printf( "Blackjack!" );
  16.    }
  17. }


Be careful to avoid division by zero in test conditions.

When possible, use integer values in test conditions.
Flowchart




Nested decisions



Pseudo code

START
   Obtain 2 cards
   Calculate sum of cards
   IF sum of cards is less than 12
      THEN
         IF first card is an ace
            THEN
               Add 10 to the sum
         END IF
   END IF
   Display the sum
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card1, card2, sum;
  6.    scanf( "%d %d", &card1, &card2 );
  7.    sum = card1 + card2;
  8.  
  9.    if ( sum < 12 )
  10.    {
  11.       if ( card1 == 1 )
  12.          sum = sum + 10;
  13.    }
  14.  
  15.    printf( "Sum of cards is %d", sum );
  16. }


The decision logic affects the order of the test conditions.

Be careful to avoid division by zero in test conditions.

When possible, use integer values in test conditions.
Flowchart




It may be possible to optimize the logic of nested decisions.

#include <stdio.h>

int main( void )
{
   int card1, card2, sum;
   scanf( "%d %d", &card1, &card2 );
   sum = card1 + card2;

   if ( sum < 12 )
   {
      if ( card1 == 1 )
         sum = sum + 10;
   }

   printf( "Sum of cards is %d", sum );
}
#include <stdio.h>

int main( void )
{
   int card1, card2, sum;
   scanf( "%d %d", &card1, &card2 );
   sum = card1 + card2;

   if ( ( sum < 12 ) && ( card1 == 1 ) )
   {
      sum = sum + 10;
   }

   printf( "Sum of cards is %d", sum );
}



Chained decisions



Pseudo code

START
   Obtain 2 cards
   Calculate sum of cards
   IF sum of cards is equal to 21
      THEN
         Display the blackjack message
      ELSE
         IF 2 cards are actually a pair
            THEN
               Suggest that the player split the pair
            ELSE
               IF sum is more than 17
                  THEN
                     Suggest that the player stand the hand
               END IF
         END IF
   END IF
   Display the sum
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card1, card2, sum;
  6.    scanf( "%d %d", &card1, &card2 );
  7.    sum = card1 + card2;
  8.  
  9.    if ( sum == 21 )
  10.       printf( "Blackjack!\n" );
  11.    else if ( card1 == card2 )
  12.       printf( "Split your cards!\n" );
  13.    else if ( sum > 17 )
  14.       printf( "Stand!\n" );
  15.  
  16.    printf( "Sum of cards is %d", sum );
  17. }


The decision logic affects the order of the test conditions.

Chained decision structures have only one path of execution.

Be careful to avoid division by zero in test conditions.

When possible, use integer values in test conditions.
Flowchart




Selection decisions with switch statement



Pseudo code

START
   Obtain 2 cards
   Calculate sum of cards
   IF sum of cards is equal to 21
      THEN
         Display the blackjack message
   END IF
   IF sum of cards is equal to 21 OR
      sum of cards is equal to 18-20
      THEN
         Suggest that the player stand the hand
   END IF
   Display the sum
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card1, card2, sum;
  6.    scanf( "%d %d", &card1, &card2 );
  7.    sum = card1 + card2;
  8.  
  9.    switch ( sum )
  10.    {
  11.       case 21 :
  12.          printf( "Blackjack!\n" );
  13.       case 20 :
  14.       case 19 :
  15.       case 18 :
  16.          printf( "Stand!\n" );
  17.       default :
  18.          printf( "Sum of cards is %d", sum );
  19.    }
  20. }


switch statements must use integer variables.

The default selection is always executed. It must be the last selection of the switch statement.

Selection decision structures may have more than one path of execution.

The decision logic affects the order of the comparison conditions. Comparisons are made in sequence from top to bottom.
Flowchart




Selection decisions with switch statement and break keyword



Pseudo code

START
   Obtain 2 cards
   Calculate sum of cards
   IF sum of cards is equal to 21
      THEN
         Display the blackjack message
   END IF
   IF sum of cards is equal to 18-20
      THEN
         Suggest that the player stand the hand
   END IF
   IF sum of cards is NOT equal to 21 AND
      sum of cards is NOT equal to 18-20
      THEN
         Display the sum
   END IF
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card1, card2, sum;
  6.    scanf( "%d %d", &card1, &card2 );
  7.    sum = card1 + card2;
  8.  
  9.    switch ( sum )
  10.    {
  11.       case 21 :
  12.          printf( "Blackjack!\n" );
  13.          break;
  14.       case 20 :
  15.       case 19 :
  16.       case 18 :
  17.          printf( "Stand!\n" );
  18.          break;
  19.       default :
  20.          printf( "Sum of cards is %d", sum );
  21.          break;
  22.    }
  23. }


The break keyword immediately exits the current switch statement.

The default selection is only executed if the switch statement has not ended. The default selection must be the last selection of the switch statement.

switch statements must use integer variables.

Selection decision structures may have more than one path of execution.

The decision logic affects the order of the comparison conditions. Comparisons are made in sequence from top to bottom.
Flowchart




Decisions may have one or more paths of execution



Chained decision structures have only one path of execution.

Selection decision structures may have more than one path of execution.




Software development techniques


Design and test the program logic with pseudo code and flow charts before implementing it with source code.

Use extra printf() functions to help follow the program logic.

Remember to check for matching braces when replacing a simple statement with a compound statement.



Iteration using while loop



Pseudo code

START
   Initialize sum to zero
   Initialize counter to zero
   WHILE counter is less than 3
      Obtain input card value
      Add input to the sum
      Increase counter by one
   END WHILE
   Display sum of all cards
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card = 0;
  6.    int sum = 0;
  7.    int counter = 0;
  8.  
  9.    while ( counter < 3 )
  10.    {
  11.       scanf( "%d", &card );
  12.       sum = sum + card;
  13.       counter++;
  14.    }
  15.  
  16.    printf( "Sum of cards is %d", sum );
  17. }


The break keyword immediately exits the current while loop.

It is possible that the statements inside a while loop is never executed because the test condition was never successful.

Be careful to avoid division by zero in test conditions.
Flowchart




Iteration using for loop



Pseudo code

START
   Initialize sum to zero
   Initialize counter to zero
   WHILE counter is less than 3
      Obtain input card value
      Add input to the sum
      Increase counter by one
   END WHILE
   Display sum of all cards
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card = 0;
  6.    int sum = 0;
  7.    int counter;
  8.  
  9.    for ( counter = 0; counter < 3; counter++ )
  10.    {
  11.       scanf( "%d", &card );
  12.       sum = sum + card;
  13.    }
  14.  
  15.    printf( "Sum of cards is %d", sum );
  16. }


The break keyword immediately exits the current for loop.

It is possible that the statements inside a for loop is never executed because the test condition was never successful.

Be careful to avoid division by zero in test conditions.
Flowchart




Iteration using do / while loop



Pseudo code

START
   Initialize sum to zero
   Initialize counter to zero
   WHILE counter is zero OR
         counter is less than 3
      Obtain input card value
      Add input to the sum
      Increase counter by one
   END WHILE
   Display sum of all cards
END


Source code

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int card = 0;
  6.    int sum = 0;
  7.    int counter = 0;
  8.  
  9.    do
  10.    {
  11.       scanf( "%d", &card );
  12.       sum = sum + card;
  13.       counter++;
  14.    } while ( counter < 3 );
  15.  
  16.    printf( "Sum of cards is %d", sum );
  17. }


The statements inside a do / while loop always execute at least once because the body of the loop executes before it reaches the test condition.

The break keyword immediately exits the current do / while loop.

Be careful to avoid division by zero in test conditions.
Flowchart