Home   MosaicPresentation format
<http://cnfolio.com/IntroToComputingTopic04Print>
Introduction to Computing – B142L

Control structures

Academic year 2011/12



Objectives:
  1. Conditional syntax in the C language
  2. Iteration syntax in the C language






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:
  1. sequential
  2. conditional
  3. repetition


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:

  1. display have a nice day
  2. if raining
  3.      display reminder to bring umbrella


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




 






Decision with if statement



Pseudo code:

  1. obtain input number
  2. if multiple of 5
  3.      display message that multiple found
  4. display remainder of division by 5


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




 






Decision with if / else statement



Pseudo code:

  1. obtain 2 cards
  2. if sum of cards equals to 21
  3.      display blackjack message
  4. else
  5.      display sum of cards


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




 






Decision with if / else statement and logical negation



Pseudo code:

  1. obtain 2 cards
  2. if sum of cards not equal to 21
  3.      display sum of cards
  4. else
  5.      display blackjack message


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




 






Nested decisions



Pseudo code:

  1. obtain 2 cards
  2. calculate sum of cards
  3. if sum of cards less than 12
  4.      if first card is an ace
  5.            add 10 to sum
  6. display the sum


The decision logic affects the order of the test conditions.

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




 



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

 






Chained decisions



Pseudo code:

  1. obtain 2 cards
  2. calculate sum of cards
  3. if sum equal to 21
  4.      display blackjack message
  5. else if 2 cards are actually a pair
  6.      suggest that the player split the pair
  7. else if sum is more than 17
  8.      suggest that player stand the hand
  9. display the 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.
Flowchart:




 






Selection decisions with switch statement



Pseudo code:

  1. obtain 2 cards
  2. calculate sum of cards
  3. evaluate value of sum
  4.      21
  5.            display blackjack message
  6.      20
  7.      19
  8.      18
  9.            suggest that player stand the hand
  10.      default
  11.           display the 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:

  1. obtain 2 cards
  2. calculate sum of cards
  3. evaluate value of sum
  4.      21
  5.            display blackjack message
  6.            break
  7.      20
  8.      19
  9.      18
  10.            suggest that player stand the hand
  11.            break
  12.      default
  13.           display the sum
  14.            break


The break keyword immediately exits the current switch statement.

The default selection is only executed if the switch statement has not ended.
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 cycle



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



Pseudo code:

  1. setup variables
  2. start loop by initializing counter to zero
  3. repeat loop if counter is less than 3
  4.      obtain input card value
  5.      add input to sum
  6. increase counter by 1 and go to test condition
  7. display sum of all cards

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



Pseudo code:

  1. setup variables
  2. start loop by initializing counter to zero
  3. repeat loop if counter is less than 3
  4.      obtain input card value
  5.      add input to sum
  6.      increase counter by 1
         (remember this step to avoid infinite loops)
  7. display sum of all cards

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 do / while loops



Pseudo code:

  1. setup variables
  2. initialize counter to zero
  3. start loop
  4.      obtain input card value
  5.      add input to sum
  6.      increase counter by 1
         (remember this step to avoid infinite loops)
  7. repeat loop if counter is less than 3
  8. display sum of all cards

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:




 






Dry run table during the software development cycle



A dry run is a manual step by step trace of the program logic and algorithm. It uses a table where the first column is the step number and one column is added for each data variable used in the program. Each statement in the program increases the dry run table by one step. Using a dry run table is useful for testing and finding errors.


Pseudo code:

  1. setup variables
  2. initialize counter to zero
  3. start loop
  4.      obtain input card value
  5.      add input to sum
  6.      increase counter by 1
  7. repeat loop if counter is less than 3
  8. display sum of all cards




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







Counter-controlled loops compared to sentinel-controlled loops



Counter-controlled loop is designed to repeat for a defined number of iterations.

Pseudo code:

  1. setup variables
  2. start loop by initializing counter to zero
  3. repeat loop if counter is less than 3
  4.      obtain input card value
  5.      add input to sum
  6.      increase counter by 1
  7. display sum of all cards

Sentinel-controlled loop is designed to repeat for a unknown number of iterations.

Pseudo code:

  1. setup variables

  2. repeat loop if card value is less than 15
  3.      add input to sum
  4.      obtain input card value

  5. display sum of all cards


Sentinel-controlled loops are widely used for event-based programming. For example:
  • repeat loop if player is sleeping
  •      increase health
  • repeat loop if accelerator is pressed down
  •      increase speed
  • repeat loop if player is walking
  •      move forward by 1







Counter-controlled loops compared to sentinel-controlled loops



Be careful of infinite loops when writing test conditions to use with sentinel-controlled loops.


Counter-controlled loop is designed to repeat for a defined number of iterations.

Pseudo code:

  1. setup variables
  2. start loop by initializing counter to zero
  3. repeat loop if counter is less than 3
  4.      obtain input card value
  5.      add input to sum
  6.      increase counter by 1
  7. display sum of all cards

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

Sentinel-controlled loop is designed to repeat for a unknown number of iterations.

Pseudo code:

  1. setup variables

  2. repeat loop if card value is less than 15
  3.      add input to sum
  4.      obtain input card value

  5. display sum of all cards

Source code:

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







Review questions




Control structures


What is the output of the program below?

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int step = 4;
  6.  
  7.    switch ( step )
  8.    {
  9.       case 1 :
  10.          printf( "One" );
  11.          break;
  12.       case 2 :
  13.          printf( "Two" );
  14.          break;
  15.       case 3 :
  16.          printf( "Three" );
  17.          break;
  18.    }
  19. }

A logic error occurs.
"Three" is displayed to output.
A runtime error occurs.
"Four" is displayed to output.






Review questions




Control structures


The program below would display C to output for which range of input values?

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int step;
  6.  
  7.    scanf( "%d", &step );
  8.    if ( step <= 200 )
  9.    {
  10.       if ( step < 100 )
  11.       {
  12.          if ( step <= 0 )
  13.             printf( "A" );
  14.          else
  15.             printf( "B" );
  16.       }
  17.       else
  18.          printf( "C" );
  19.    }
  20.    else
  21.       printf( "D" );
  22. }

step <= 100
100 <= step <= 200
0 < step < 100
step <= 0






Review questions




Control structures


How many times will the body of the loop repeat in the program below?

  1. #include <stdio.h>
  2.     
  3. int main( void )
  4. {
  5.    int num1 = 0;
  6.    int num2 = 0;
  7.    int sum = 0;
  8.    int counter = 0;
  9.    int input;
  10.  
  11.    while ( counter < 50 )
  12.    {
  13.       scanf( "%d", &input );
  14.       sum = sum + input;
  15.  
  16.       if ( input >= 0 )
  17.          num2 = num2 + 1;
  18.       else
  19.          num1 = num1 + 1;
  20.  
  21.       counter = counter + 1;
  22.    }
  23. }

49
Once
None
50






Review questions




Control structures


What is the best description of the variable num1 in the program below?

  1. #include <stdio.h>
  2.     
  3. int main( void )
  4. {
  5.    int num1 = 0;
  6.    int num2 = 0;
  7.    int sum = 0;
  8.    int counter = 0;
  9.    int input;
  10.  
  11.    while ( counter < 50 )
  12.    {
  13.       scanf( "%d", &input );
  14.       sum = sum + input;
  15.  
  16.       if ( input >= 0 )
  17.          num2 = num2 + 1;
  18.       else
  19.          num1 = num1 + 1;
  20.  
  21.       counter = counter + 1;
  22.    }
  23. }

Sum of all positive input values
Sum of all negative input values
Count of all negative input values
Count of all positive input values