<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 #include   int main( void ) {    int raining = 1;      printf( "Have a nice day\n" );      if ( raining ) /* This is the test condition */       printf( "Bring an umbrella" ); } 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 #include   int main( void ) {    int number;      scanf( "%d", &number );      if ( ( number % 5 ) == 0 )    {       printf( "Number is multiple of 5\n" );    }      printf( "The remainder is %d", number % 5 ); } 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 #include   int main( void ) {    int card1, card2, sum;    scanf( "%d %d", &card1, &card2 );    sum = card1 + card2;      if ( sum == 21 )    {       printf( "Blackjack!" );    }    else    {       printf( "Sum is %d", sum );    } } 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 #include   int main( void ) {    int card1, card2, sum;    scanf( "%d %d", &card1, &card2 );    sum = card1 + card2;      if ( sum != 21 )    {       printf( "Sum is %d", sum );    }    else    {       printf( "Blackjack!" );    } } 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 #include   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 ); } 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 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 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 #include   int main( void ) {    int card1, card2, sum;    scanf( "%d %d", &card1, &card2 );    sum = card1 + card2;      if ( sum == 21 )       printf( "Blackjack!\n" );    else if ( card1 == card2 )       printf( "Split your cards!\n" );    else if ( sum > 17 )       printf( "Stand!\n" );      printf( "Sum of cards is %d", sum ); } 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 #include   int main( void ) {    int card1, card2, sum;    scanf( "%d %d", &card1, &card2 );    sum = card1 + card2;      switch ( sum )    {       case 21 :          printf( "Blackjack!\n" );       case 20 :       case 19 :       case 18 :          printf( "Stand!\n" );       default :          printf( "Sum of cards is %d", sum );    } } 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 #include   int main( void ) {    int card1, card2, sum;    scanf( "%d %d", &card1, &card2 );    sum = card1 + card2;      switch ( sum )    {       case 21 :          printf( "Blackjack!\n" );          break;       case 20 :       case 19 :       case 18 :          printf( "Stand!\n" );          break;       default :          printf( "Sum of cards is %d", sum );          break;    } } 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 #include   int main( void ) {    int card = 0;    int sum = 0;    int counter = 0;      while ( counter < 3 )    {       scanf( "%d", &card );       sum = sum + card;       counter++;    }      printf( "Sum of cards is %d", sum ); } 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 #include   int main( void ) {    int card = 0;    int sum = 0;    int counter;      for ( counter = 0; counter < 3; counter++ )    {       scanf( "%d", &card );       sum = sum + card;    }      printf( "Sum of cards is %d", sum ); } 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 #include   int main( void ) {    int card = 0;    int sum = 0;    int counter = 0;      do    {       scanf( "%d", &card );       sum = sum + card;       counter++;    } while ( counter < 3 );      printf( "Sum of cards is %d", sum ); } 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 