Introducing Control Structures

 

It is now time to put a little magic in our logic and programs. So far our programs have been very safe and very simple. We have yet to stretch your ability or creativity with complex logic. That will all change with this lesson as we introduce control structures. We can now bring together the techniques covered in lesson's one through three with program control structures to solve problems we have conveniently ignored up to this point. Control structures will allow us to add two critical functions into logic. Specifically, these two missing pieces are decision making and repetition. These two control structures will give us the power to do far more complex processing and provide flexibility with logic that I have only hinted at up to this point.

 

The logic instructions used so far have been straightforward and were absent of more sophisticated logic necessary for our program to solve complex problems. In the previous lessons, we have mentioned if statements and while loops but have yet to explain why they are used and implemented. Why are they are so important to programming? I might have been guilty of teasing you a little, but there is a method to this approach and also some madness. I wanted to focus your attention in the first section of the lesson on the importance of creating complete logic models. Logic models are often overlooked in programming classes or there is an assumption that the student already understands these concepts. The absence of a complete logic model leads to unnecessary code rework or worst yet a solution that is different than what was requested.

 

Key Concept: Professional computer trade publications are littered with stories of computer programs gone bad. Usually a lot of time and money was spent on a solution that was different than what the customer wanted. This is usually caused by the programmer not listening carefully to the end user or short cutting on logic design. Programmers are people and people have mis and/or pre-conceptions about how things should work. It is very easy to get lost in the moment and assume rather than going back and verifying the solution. A good logic model that is validated with the customer is a great insurance policy in protecting the program from coming out different then what was requested.

 

When modeling logic for a typical business problem, it does not take long before our models need to include some element of decision-making. As we have already seen in the Cactus Books and Tapes case studies, our ordering program processes data differently if our customer purchases more than three books. Whereas our previous logic examples have ignored the requirement for our program to include a decision, we can now build logic to include decision structures that will allow our program to branch down a thread of program code. In this lesson, we will learn how to implement techniques of structured programming. By using structured programming techniques, we will not only improve the quality of our logic but make programs easier to write, easier to debug, easier to understand and easier to maintain.

 

In Practice: Procedural Programming - We will officially introduce procedural programming next lesson when we cover the topic of modularization but is appropriate to mention it with structures since it is a critical part of structured programming. When we develop programs using procedural programming techniques we implement our logic as procedures. Procedures, like structures, are blocks of code that represent some logic component of the entire program. For example, we might create a procedure to do calculate sales tax and another procedure to calculate quantity discounts. Both procedures might be necessary to create a customer invoice but both operations stand on there own and can stand alone as logic inside a program. Structures are typically written as blocks of statement and are similar to procedures in that many times they stand alone and represent a piece of the entire solution.

 

Structured Programming: Simply stated, structured programming is the implementation of three control structures. Structures can be thought of as blocks of code that allows programs to implement sequence, decisions and repetition. Sequence, decision and repetition structures each have one entry point and one exit point which provide an element of simplification and organization that makes programs easier to understand and maintain. All programming languages make use of these three structures in various ways. They are essential to performing the complex logic necessary in computer programming.

 

With control structures (sequence, decision and repetition), we can also begin to represent a task as a series of related instructions. We can begin to look at some logic tasks as a series of related steps. For example, when use an if statement to implement a decision structure, the if statement has two branches which describe the logic outcomes of the Boolean expression. The Boolean expression consists of the comparison of two values which results in a true or false value returned. For example the Boolean expression (1 < 2) is true but (2<1) would be false. The outcomes are organized and defined within the if code block (or we could say within the control decision structure).

 

An if statement in pseudo code would look like this:

 

if (1 < 2) then
print "This is my true outcome"
else
print "This is my false outcome"

 

The then keyword marks the start of the true branch and the else marks the start of the false branch.

 

For example, if the customer purchased more than three books, a series of calculations and statements will be executed to derive the correct discounted cost. The cost calculation for the customer who purchased less than three books is different then the customer who purchased 5 books. Each possible scenario required a different set of calculations and statements. The control structure allows us to group more than one logic step into a group which represented some particular task. In this example, the task is the calculation of cost.

 

 

Figure 1: A decision control structure - If Block

 

If you're a customer visiting that bookstore and you purchased five books you would certainly expect the calculations to reflect the quantity of books purchased. If the computer software can not support this level of logic flexibility, the program is not very useful. Control structures will help us meet this challenge.

 

Control Structures: Programming languages that support complex logic (i.e. like logic decisions) will implement sequence, decision and repetition structures. These structures will provide the programming flexibility necessary for computer programs to solve complex problems. Control structures enable the grouping of processing steps, the implementation of decisions and the use of repetition in our logic and then our programs.

 

What will remain the same from previous lessons is the importance of proven programming processes. We will still use the program development process to first create a logic model, then create our programming code, next test our program and finally implement the program. What will be different is that our logic solutions (and therefore our programs) will include sequence, decision, repetition structures.

 


Click to close