The basic program control structure concept consists of 4 elements:
- Sequence - by default, program instructions are executed from to bottom.
- Selection - whenever a decision is required, selection control is implemented i.e. program will branch into two or more paths and the program decides the path selection based on certain math/logic statement evaluation.
- Loop - whenever there is a need to repeat certain instructions, loop control is implemented.
- Module - related instructions are grouped into modules for better code management and code readability.
SEQUENTIAL
# SEQUENTIAL # 01.hello world print("hello world") print("\n====================\n")
# SEQUENTIAL # 02.print 1+2 print(1+2) print("\n====================\n")
# SEQUENTIAL # 03.input, print add var var1=int(input("var1:")) var2=int(input("var2:")) print("\n====================\n") print("%d + %d = %d" % (var1,var2,var1+var2)) print("\n====================\n")
# SEQUENTIAL # 04.input, print mul var print("%d * %d = %d" % (var1,var2,var1*var2)) print("\n====================\n")
SELECTION
# IF-SELECTION # 05.input, print div var if (var2==0): print("Error. Division by 0.") else: print("%d / %d = %f" % (var1,var2,(var1/float(var2)))) print("\n====================\n")
LOOP
# FOR-LOOP # 06. loop from 0 to 2 print ("[06] Start counting.") for x in range(3): print(x) else: print("Done.") print("\n====================\n")
# LOOP # 07. loop from 0 to 4, display loop counter listArr = [1,2,3,4,5] print ("[07] Displaying listArr values.") for x in range(len(listArr)): print(listArr[x]) else: print("Done.") print("\n====================\n")
# LOOP # 08. input index and value, update list listArr = [1,2,3,4,5] print ("[08A] Displaying listArr values.") for x in range(len(listArr)): print(listArr[x]) else: print("Done.") indexNumber = int(input ("Enter index number:")) newValue = int(input ("Enter new value:")) listArr[indexNumber] = newValue print ("[08B] Displaying listArr values.") for x in range(len(listArr)): print(listArr[x]) else: print("Done.") print("\n====================\n")
# MODULE # 09. Modular program # displayData, updateData, and displayData again # subModule_displayData def displayData(listArr): for x in range(len(listArr)): print(listArr[x]) else: print("Done.") print("\n====================\n") # subModule_updateData def updateData(listArr): indexNumber = int(input ("Enter index number:")) newValue = int(input ("Enter new value:")) listArr[indexNumber]=newValue print("\n====================\n") # mainModule listScore = [10,20,30,40,50] print ("[09A] Displaying score values.") displayData(listScore) print ("[09B] Input new scores.") updateData(listScore) print ("[09C] Displaying updated score values.") displayData(listScore)
0 Comments