Make your own free website on Tripod.com

Tutorial 1

An Introduction to Control Structures

Overview

This tutorial covers the three basic control structures common to most programming languages. These are the sequence, selection, and repetition structures. While how these control structures are implemented in C++ are covered in subsequent tutorials, it is imperative that students understand how and why each control structure is used.

Objectives

After completing the tutorial, the student will be able to:

  • Explain the sequence, selection, and repetition structures
  • Write simple algorithms using the sequence, selection, and repetition structures

Lecture Notes

Algorithm

An algorithm is the step-by-step instructions that accomplish a task. While algorithms consist of one or more of the three basic control structures, it must be noted that algorithms should not consist of an infinite amount of steps, but rather a finite number of steps. Your algorithm is the single most important step after defining the problem in the process of solving a problem. For regardless of which computer language is being used, if your algorithm does not solve the problem, then your task will not be successfully completed. Remember, the design or your solution is everything.

Control Structures

The three basic control structures used in computer programming are the sequence, selection, and repetition structures. The sequence structure directs the computer to process the program instructions, one after another, in the order in which they are listed. The sequence structure is the most simple of the basic control structures and is common to all computer programs.

The remaining two control structures, selection and repetition, may or may not be present in a given computer program dependent upon how the programmer wishes to solve the problem. The repetition structure, commonly called iteration, directs the computer to repeat one or more program instructions until some condition is met. This condition may be checked at the beginning or end of the set of instructions to be processed dependent upon the language being used. The final control structure is selection, commonly called the decision structure. The selection structure directs the computer to make a decision (evaluate a condition), and then take an appropriate action based upon that decision.

Additional Activities

Solutions to Questions

1. d sorting                 6. c selection

2. c sequence             7. b selection

3. d sequence             8. a repetition

4. a an algorithm         9. c both repetition and selection

5. b repetition

Solutions to Concept Lesson Exercises

  1. 1. repeat 5 times:

walk

2. jump

3. repeat 10 times:

walk

4. turn

5. sit

 

2.     1. repeat 5 times:

walk

        2. if the box is red, do this:

jump

otherwise, do this:

throw the box out of the way

        3. repeat 10 times:

walk

        4. turn

        5. sit

 

  1. Version 1:
  2. 1. repeat for each employee:

    read the salary and years employed

    if the years employed are greater than or equal to 5, do this:

    calculate the bonus by multiplying the salary by 2%

    otherwise, do this:

    calculate the bonus by multiplying the salary by 1%

    print the bonus

    Version 2:

    1. repeat for each employee:

    read the salary and years employed

    if the years employed are less than 5, do this:

    calculate the bonus by multiplying the salary by 1%

    otherwise, do this:

    calculate the bonus by multiplying the salary by 2%

    print the bonus

     

  3. Version 1:

  1. read the customerís age and item price

                            2. if the customerís age is greater than or equal to 65, do this:

                                    assign 10% as the discount rate

otherwise, do this:

                                   assign 0 as the discount rate

3. calculate the amount due by multiplying the item price by (1 minus the

discount rate)

                        4. print the amount due

Version 2:

1. read the customerís age and item price
2. if the customerís age is less than 65, do this:

            assign 0 as the discount rate

        otherwise, do this:

            assign 10% as the discount rate

3. calculate the amount due by multiplying the item price by (1 minus the discount rate)

4. print the amount due

 

5.         1. divide 12 by 2, giving 6

            2. multiply 3 by 2, giving 6

            3. add 6 and 6 giving 12

            4. subtract 3 from 12 giving 9

 

6.         1. divide y by z

            2. multiply the result of step 1 by 3

            3. add x to the result of step 2

 

Solutions to Application Lesson Exercises

  1. repeat until you are directly in front of the box:

  2. walk (inside loop|selection)

    drop the toy in the box (outside loop|selection)

     

  3. stand (outside loop|selection)

  4. repeat 4 times:

    walk (inside loop|selection)

    pick up the ball (outside loop|selection)

    turn (outside loop|selection)

    repeat 4 times:

    walk (inside loop|selection)

    turn (outside loop|selection)

    sit (outside loop|selection)

     

  5. repeat until you are directly in front of the chair:

  6. walk (inside loop|selection)

    if the chair is broken, then do this: (outside loop)

    fix the chair (inside loop|selection)

    otherwise, do this: (outside loop)

    turn (inside loop|selection)

    sit (inside loop|selection)

     

  7. stand (outside loop|selection)

repeat until you are directly in front of the box:

walk (inside loop|selection)

if the box is not full, then do this: (outside loop)

drop the toy in the box (inside loop|selection)

turn (outside loop|selection)

repeat until you are directly in front of the chair:

walk (inside loop|selection)

turn (outside loop|selection)

sit (outside loop|selection)

 

5. 1. repeat 25 times:

            read the studentís answer and the correct answer

if the studentís answer is not the same as the correct answer, do this:

mark the studentís answer incorrect

 

6. 1. repeat for each component name on the list:

read the component name from the list

search the package for the component

if the component was received, do this:

cross the component name off the list

otherwise, do this:

circle the componentís name on the list

 

7. Changes made to the algorithm are shaded in the figure.

  1. repeat 5 times:

read the employeeís name, hours worked, and pay rate

if the hours worked are not greater than 40, do this:

calculate the gross pay by multiplying the hours worked by the pay rate

otherwise, do this:

calculate the overtime hours by subtracting 40 from the number of hours worked

calculate the overtime pay by multiplying the overtime hours by the pay rate divided by 2

calculate the gross pay by multiplying the hours worked by the pay rate, and then adding the overtime pay to the result

print the employeeís name and gross pay

 

8.                     1. subtract 2 from 4, giving 2

                        2. divide 12 by 2, giving 6

                        3. multiply 3 by the result of step 1, giving 

          4. add the result of step 2 to the result of step 3, giving 12

          5. add the result of step 4 to 1, giving 13

9.                                     1. repeat 3 times:

                                                        walk

                                        2. turn left 90 degrees

                                        3. repeat 2 times:

                                                        walk

                                        4. turn right 90 degrees

                                        5. repeat 3 times:

                                                        walk

                                        6. turn left 90 degrees

                                        7. walk

                                        8. turn right 90 degrees

                                        9. repeat 4 times:

                                                        walk

                                        10. turn around 180 degrees

                                        11. sit

 

10. Changes made to the algorithm are shaded in the figure.

                    1. walk into maze

                    2. turn left 90 degrees

                    3. repeat until you are directly in front of a wall:

                                walk

                    4. turn right 90 degrees

                    5. repeat until you are directly in front of a wall:

                                walk

                    6. turn right 90 degrees

                    7. repeat until you are directly in front of a wall:

                                walk

                    8. turn left 90 degrees

                    9. repeat until you are directly in front of a wall:

                                walk

                    10. turn left 90 degrees

                    11. repeat until you are directly in front of a wall:

                                walk

                    12. turn right 90 degrees

                    13. repeat until you are directly in front of a wall:

                                walk

                    14. turn left 90 degrees

                    15. repeat until you are directly in front of a wall:

                                walk

                    16. turn right 90 degrees

                    17. repeat until you are out of the maze:

                                walk