Since the invention by Von Neumann of the stored program computer, it was recognized that a tremendous potential of computing equipment was the ability to alter its behavior, depending on the input data. Calculating machines had, for some time, been able to perform fixed arithmetic operations on data, but the potential of machines capable of making decisions opened up many new possibililities. Machines that could make decisions were capable of sorting records, tabulating and summarizing data, searching for information, and many more advanced operations that could not even be imagined at the time.
In early programming languages, like Fortran (first invented in 1954) and various low level machine languages, the goto statement allowed the computer to deviate from the sequential execution of the program instructions. The goto statement was recognized to be a very powerful construction, and soon, programs of increasing complexity and power were developed.
However, the increasing complex code became harder and harder to maintain. Dijkstra, in 1966, was one of the first persons to recognize that this run away complexity of programs was due to the overuse of the goto statement (Dijkstra, E. W., "Go To Considered Harmful," Communications of the ACM, March 1966). In fact, it was determined shortly thereafter, that the goto statement is not needed at all. This was the birth of the discipline of Structured Programming.
Python does not have a goto statement.
Add flour Add salt Add yeast Mix Add water Knead Let rise Bake
stack dishes by sink fill sink with hot soapy water while there are more dishes get dish from counter wash dish put dish in drain rack end wipe off counter rinse out sink
compute course_score if course_score is greater than or equal to 90 assign course grade to A elsif course_score is greater than or equal to 75 assign course grade to B elsif course_score is greater than or equal to 60 assign course grade to C elsif course_score is greater than or equal to 50 assign course grade to D else assign course grade to D end submit course grade to registrar
get mail from mailbox put mail on table while more mail to sort get piece of mail from table if piece is personal read it elsif piece is magazine put in magazine rack elsif piece is bill pay it elsif piece is junk mail throw it in wastebasket end end
Structured programming is a program written with only the three constructions sequence, decision (if..elif statements), and repetition (while or for statements). Important: the body of a Python if, elif, while, or for statement is indicated by indenting four spaces. Python does not use end statements.
x = 56 y = 11 z = x + y print(z)
if condition: actionExample:
if x % 2 == 0: print("The number is even.")
while condition: actionExample:
n = 1 while n < 100: print(n) n = n * n
To make programs easier to read, some additional constructs were added to the basic three original structured programming constructs:
for element in a range action endExample:
for x in range(1, 5): print(x, end="")Important: A Python range is inclusive for the lower limit, but exclusive for the upper limit. range(1, 5) actually goes from 1 to 5 - 1 = 4. This range consists of the values [1, 2, 3, 4].
if condition1: action1 elif condition2: action2 elif condition3: action3 else default_action:Example:
n = int(input("Enter a positive integer: ")) if n == 1: print "one" elsif n == 2 print "two" elsif n == 2 print "three" else: print "many"