Building Blocks of Algorithm
Building Blocks of Algorithm
An Algorithm is made up of three basic building blocks:
- Sequence
- Selction
- Iteration
Sequence
Sequential control means that the steps of an algorithm are carried out in a sequential manner, where each step is executed exactly once.
Example: Algorithm to convert Centigrade to FahrenheitStep 1: Read the temperature in degree CentigradeStep 2: Convert the Centigrade to Fahrenheit using the formulaF= 9/5 * c+32Step 3: Print the Celsius and Fahrenheit value
Selection
Algorithms can use selection to determine a different set of steps to execute based on a Boolean expression.If the conditional test is true, one part of the algorithm will be executed, otherwise it will execute the other part of the algorithm.
# Example: Algorithm to find Greatest of two numbersStep 1: Read first numbers AStep 2: Read second number BStep 3: IF(A>B) thenPrint A is bigElsePrint B is big
Iteration
Algorithms often use repetition to execute steps a certain number of times or until a certain condition is met.
Example: Print 'Hello world' 5 timesStep 1 : StartStep 2 : Initialize the value of i as 1Step 3 : Check the condition i less than or equal to 5, if the condition is true goto step 3.1 else goto step 4Step 3.1: Print “Hello World” and incrementthe value of i by 1Step 3.2: Repeat the Step 3 until the condition is trueStep 4 : Stop
Youtube
References
- Allen B. Downey, “Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated for Python 3, Shroff/O‘Reilly Publishers, 2016 (http://greenteapress.com/wp/thinkpython/)
- Guido van Rossum and Fred L. Drake Jr, ―An Introduction to Python – Revised and updated for Python 3.2, Network Theory Ltd., 2011.
- John V Guttag, ―Introduction to Computation and Programming Using Python‘‘, Revised and expanded Edition, MIT Press , 2013
- Robert Sedgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
- Timothy A. Budd, ―Exploring Python‖, Mc-Graw Hill Education (India) Private Ltd.,, 2015. 4. Kenneth A. Lambert, ―Fundamentals of Python: First Programs‖, CENGAGE Learning, 2012.
- Charles Dierbach, ―Introduction to Computer Science using Python: A Computational Problem-Solving Focus, Wiley India Edition, 2013.
- Paul Gries, Jennifer Campbell and Jason Montojo, ―Practical Programming: An Introduction to Computer Science using Python 3‖, Second edition, Pragmatic Programmers, LLC, 2013.