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. Sequence

Example: Algorithm to convert Centigrade to Fahrenheit
Step 1: Read the temperature in degree Centigrade
Step 2: Convert the Centigrade to Fahrenheit using the formula
F= 9/5 * c+32
Step 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.

Selection

# Example: Algorithm to find Greatest of two numbers
Step 1: Read first numbers A
Step 2: Read second number B
Step 3: IF(A>B) then
Print A is big
Else
Print B is big

Iteration

Algorithms often use repetition to execute steps a certain number of times or until a certain condition is met.

Iteration

Example: Print 'Hello world' 5 times
Step 1 : Start
Step 2 : Initialize the value of i as 1
Step 3 : Check the condition i less than or equal to 5, if the condition is true goto step 3.1 else goto step 4
Step 3.1: Print “Hello World” and increment
the value of i by 1
Step 3.2: Repeat the Step 3 until the condition is true
Step 4 : Stop

Youtube

Building Blocks of Algorithm

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.