COSC 1336 | PyThon

ACC

Sequence-Selection-Repetition

Foreword

All programming languages have these three building blocks in common: sequence, selection, and repetition. These three control structures were first formally proposed in the Structured Program Theorem. All solutions are built using these simple constructs. In this chapter, we take a brief look at each of these. Selection and repetition will be addressed more thoroughly in subsequent chapters.

 

Sequence

The sequence construct is the simplest and is so obvious it almost 'goes without saying'. It consists of performing one action, then another, then another, and so on. It is performing one step after another in a sequence. That's it. All of the program examples we have reviewed thus far have consisted primarily of the sequence construct. Let's look at another example. Create a new project in PyCharm called project-three-constructs. Create a new file, name it sequence.py, and code it as shown. Each of the lines in this example represent a single step. Line 1 is a comment and therefore not processed by the interpreter. Line 3 is step one and line 4 is step two. Lines 6 and 7 are steps three and four. The remaining steps are shown as the print statements.

 

Here's the output.

 

The term 'sequence' in this chapter is used in the generic sense, as it applies to any programming language. It is not to be confused with the 'sequence' types in Python. Sequence types include strings, lists, etc. Sequence types will be covered thoroughly in subsequent chapters. See this link for more about sequence types.

 

Selection

Selection is the second of the three constructs and is not much more complex than sequence, at least in its simplest form. Selection consists of making a decision. iPhone or Galaxy, left or right, Aspen or Vail? Create a new file in the project-three-constructs project and name the file selection.py. Code it as shown.

On line 2 the user is asked to enter a favorite color. Lines 5-11 contain an 'if statement' which is one of two selection constructs in the program. In that statement, the question is posed, 'if fav_color is equal to Yellow and fav_number is equal to 77 proceed to line 6'. If fav_color is not equal to Yellow and/or fav_number is not equal to 77, then lines 6-7 will be skipped and the program flow will proceed to line 8 and so on. By the way, note that parentheses are not required around the condition on line 5, 16, or 18. Many languages require conditions to be enclosed in parentheses but not Python.

See the two output examples below. In the first, the user entered Yellow and 77 lines 6-7 were processed. Confirm that both lines were printed in the output and by running the code yourself. In the second output, the user enter Blue and 9 and lines 6-7 were skipped but lines 9-11 were processed.

Note that the indentation of lines makes those lines part of the block. For instance, lines 6-7 and 9-11 are indented the same amount and are therefore part of the 'if statement'. Most other languages use braces {} to define program blocks (separate sections of code). Python uses indentation to define program blocks. We will see more examples of this in subsequent chapters.

Line 16 contains another if statement which states 'If first_number is less than second_number then do the statement on line 17'. If first_number is not less than second_number then line 18 is processed and that condition is evaluated. On line 18, if first_number is also not greater than second_number then line 20 is processed. Line 20 is called a 'trailing else'. The statement(s) in the trailing else block will be executed if none of the statements above are true. So, in this case, if neither the < or > condition is true, then the == condition must be true.

 

Here's the output when Yellow is input as the favorite color.

 

Here's the output when Blue is input as the favorite color and 9 is entered as the favorite number. Notice that the body of the 'if' statement on lines 5 and 6 is skipped.

 

Repetition

As the name suggests, repetition entails performing steps repeatedly until a condition is met. Repetition is also known as iteration. When performing repetition, the program may reserve seats until all have been selected, pump fuel until the full level has been reached, run a compressor until a temperature has been achieved, etc. Like the other two constructs, in its simplest form, repetition should be fairly easy to understand. Create a new file in the project-three-constructs project and name the file repetition.py. Code it as shown.

On line 5, the user is asked if there are strawberry sales to enter. This type of entry before a loop that uses the information (more_sales_data) in the condition (more_sales_data == "y") is known as a 'priming read'. The value of more_sales_data must be given an initial value before encountering the beginning of the loop on line 13. A list data type is created on line 11 to store data on line 17. The contents of the list are displayed on lines 29-30.

What would happen if line 5 were commented out and therefore not run? Try it. You will see those two words, 'Try it', many times throughout the course. To learn to code, it is essential to have the "four C's" of the modern software developer - curiosity, capability, confidence, and courage to try new approaches and ideas on a regular basis.

The while statement on line 13 represents a loop which consists of lines 13-24. The statement is read as 'while more_sales_data is equal to y, perform the steps in the loop.' Notice that if the user enters 'y' when line 5 is executed, s/he intends to execute the loop. Therefore, more_sales_data will equal 'y' the first time line 13 is encountered and the loop will be processed. Line 14 asks the user to enter a number for sales in pints. The int() built-in function on line 14 converts the keyboard entry into an integer so it can be used in the calculation on line 20. What would happen if the built-in function int() were removed from line 14? Try it.

Line 20 uses a compound addition operator (+=) which is shorthand for writing:

total_sales_data = total_sales_data + sales

The variable total_sales_data is known as an accumulator (or running total) since it accumulates the new_sales_data values each time the loop is processed. It is initialized to 0 on line 8.

Recall that line 5 was known as a priming read. The value for more_sales_data is obtained from the user. When we see a priming read outside of the loop, then, in almost all cases a very similar statement will also exist near the bottom of the loop to ask the user if s/he wishes to continue.

Notice that line 23 is almost identical to line 5. As long as the user enters 'y' on line 23, the condition on line 13 will be true, and the loop will execute again. When the user enters a value other than 'y' in response to the question on line 23, the condition on line 13 will be false and the loop will end. The accumulator is output on line 32 to display the total sales in pints that were entered. A 'for loop' is used on lines 29-30 to output the contents of the list that were appended each time through the 'while loop' on line 17.

 

Here's the output.