Pseudo Code Example of Sequence Structures
A sequence structure is represented in pseudo code as a line of instruction. We have discussed in earlier lessons the relationship between pseudo code and flowcharts. A pseudo code statement representing sequence would typically contain text very similar to what is found within the rectangle of the flowchart. Below, I have included two examples of sequence pseudo code.
message equals "Hello World" profit equals sales less cost
Background Information: Where Program Structures Fit Historically and Today - Structured programming became popular in the nineteen seventies as a reaction to poorly constructed programs. Poorly constructed programs were difficult and time consuming to maintain. Time is money and programs that are poorly written take longer to develop and maintain. Structured programming methodologies used structures to organize logic and make it easier to maintain. Programmers using these techniques were more effective and could do more in less time. This was a considered a great benefit to the organization. The development of structured programming techniques was one of the first developments that encouraged the training of disciplined programmers who developed programs using standardized proven professional techniques.
Using Decision Structures
Sequence structures are certainly an important part of program logic but decision structures bring a flexibility and power to our programs that really make programming worth while. It is seldom the case that the stepwise format of sequence structures alone can handle the complexity necessary for a program to solve even a very simple problem.
Decision structures are typically implemented with if statements. If statements will always contain a Boolean expression that will consist of logical and compound operators combined with identifiers and expressions. When evaluated, the boolean expression will return a value of true or false. If statements support decisions by providing a branching of code via the evaluation of a Boolean expression. If the Boolean expression is true, the true branch is followed and a set of instructions associated with the true branch is executed. An if statement can optionally execute a separate set of instructions (identified with the else keyword) if the Boolean expression value is false.
Bottom line, a single decision structure implemented with an if statement can only have two outcomes. One if branch is executed when the Boolean expression is true and one if branch is when the expression is false. By combining decisions structures with other control structures (this process is referred to as nesting) or sequence and repetition structures, we can create complex logic within our program to handle a variety of decision scenarios. We will show the power and flexibility of nesting control structures later in the lesson.
If statements can be used in a variety of formats and based on the decision that needs to be implemented. Along with if statements, many languages support the case key word which is another alternative when implementing a decision that has multiple outcomes. We will spend time on both of these topics with flowchart and pseudo code examples of each.
Figure 7: Single Branch And Two Branch If Statements.
Single branch IF statements
If statements can be implemented two different ways. An if statement can be implemented with a single true branch (true), and if statement can be implemented with two branches (true and false). We will talk about single branch if structures first since they are typically the easiest and least complicated to use.
Single Branch IF Structures: Decision structures are usually implemented as if statements and a single branch if statement provides for a block of instructions to be executed if the associated Boolean expression evaluates as true. It is a requirement of an if block to contain at least a true branch. This if statement does not respond to a false decision.