# Selection and Repetition

Selection

The branch refers to a binary decision based on some condition. If the condition is true, one of the two branches is explored; if the condition is false, the other alternative is taken. This is usually represented by the “if-then” construct in pseudo-codes and programs. In flowcharts, this is represented by the diamond-shaped decision box. This structure is also known as the selection structure.

1. Write algorithm to find the greater number between two numbers.

Step1: Start
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End

2. An algorithm to find the largest value of any three numbers.

Step1: Start
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5:If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7: End

Repetition

The loop allows a statement or a sequence of statements to be repeatedly executed based on some loop condition. It is represented by the ‘while’ and ‘for’ constructs in most programming languages, for unbounded loops and bounded loops respectively. (Unbounded loops refer to those whose number of iterations depends on the eventuality that the termination condition is satisfied; bounded loops refer to those whose number of iterations is known before-hand.) In the flowcharts, a back arrow hints the presence of a loop. A trip around the loop is known as iteration. You must ensure that the condition for the termination of the looping must be satisfied after some finite number of iterations, otherwise it ends up as an infinite loop, a common mistake made by inexperienced programmers. The loop is also known as the repetition structure.

1. An algorithm to calculate even numbers between 0 and 99

Step1. Start
Step2. I = 0
Step3. Write I in standard output
Step4. I = I+2
Step5. If (I <=98) then go to line 3
Step6. End

2. Design an algorithm which gets a natural value, n as its input and calculates odd numbers equal or less than n. Then write them in the standard output:

Step1. Start
Step3. I ← 1
Step4. Write I
Step5. I ← I + 2
Step6. If (I <= n) then go to line 4
Step7. End

3. Design an algorithm which generates even numbers between 1000 and 2000 and then prints them in the standard output. It should also print total sum:

Step1. Start
Step2. I = 1000 and S = 0
Step3. Write I
Step4. S = S + I
Step5. I = I + 2
Step6. If (I <= 2000) then go to line 3
else go to line 7
Step7. Write S
Step8. End

4. Design an algorithm with a natural number, n, as its input which calculates the following formula and writes the result in the standard output:

S = ½ + ¼ + … +1/n
Step1. Start
Step3. I ← 2 and S ← 0
Step4. S= S + 1/I
Step5. I ← I + 2
Step6. If (I <= n) then go to line 4
else write S in standard output
Step7. End

Combining the use of these control structures, for example, a loop within a loop (nested loops), a branch within another branch (nested if), a branch within a loop, a loop within a branch, and so forth, is not uncommon. Complex algorithms may have more complicated logic structure and deep level of nesting, in which case it is best to demarcate parts of the algorithm as separate smaller modules. Beginners must train themselves to be proficient in using and combining control structures appropriately, and go through the trouble of tracing through the algorithm before they convert it into code.

Algorithms executed by a computer can combine millions of elementary steps, such as additions and subtractions, into a complicated mathematical calculation.

Also by means of algorithms, a computer can control a manufacturing process or coordinate the reservations of an airline as they are received from the ticket offices all over the country. Algorithms for such large-scale processes are, of course, very complex, but they are built up from pieces.

One of the obstacles to overcome in using a computer to solve your problems is that of translating the idea of the algorithm to computer code (program). People cannot normally understand the actual machine code that the computer needs to run a program, so programs are written in a programming language such as C or Pascal, which is then converted into machine code for the computer to run.