Many times we want to do computation where a condition needs to be checked several times.
we do it using loop statements.
Figure 1.7: While loop diagram
There are many types of decision trees used in programming languages. There are Switch
statements, loop statements etc. In Switch statements, many conditions can be checked one
after another. For example, if we need to do different computation for each age group in
which the leader belongs then we can use Switch statement.
The loops are used for testing a condition for a range of test values. For example, suppose
we need to find out age of each person in the group holding chocolates and then perform
some computation then we will need a loop. First we may need to find age of each person
and then do some computation if the age matches some specified value. Figure 1.7 depicts a
while loop. As long as a condition is found true, some computation is done in each cycle of
the loop. After run of each cycle through the loop we may have to increment the condition
and then check the condition again with increased or decreased value of condition statement.
When after further increment or decrement the condition is no longer true and evaluates to
false then the computation will be stopped and we can exit the loop.