In this post control structures used in programming, control structure types, control structures in c++, control structures in java, control structures in python, control structures in c programming pdf.
Control Structures Used in Algorithm
An algorithm has a finite number of steps. Some steps may involve decision-making and repetition Broadly speaking, an algorithm may employ one of the
Following control structures:
By sequence, we mean that each step of an algorithm is executed in a specified order. Let us write an algorithm to add two numbers. This algorithm performs the steps in purely sequential order, as shown in Fig.
Decision statements are used when the execution of a process depends on the outcome of some condition. For
example, if x=y, then print EQUAL. So the general form of IF construct can be given as:
IF condition Then process
A condition in this context is any statement that may evaluate to either a true value or a false value. In the above example, a variable x can be either equal to y or not equal to y. However, it cannot be both true and false. If the condition is true, then the process is executed. A decision statement can also be stated in the following manner:
Then process 1
ELSE process 2
This form is popularly known as the IF-ELSE construct. Here, if the condition is true, then process 1 is executed, else process 2 is executed. The below Figure shows an algorithm to check if two numbers are equal.
Repetition, which involves executing one or more steps a number of times, can be implemented using constructs such as while, do-while, and for loops. These loops execute one or more steps until some condition is true. The below Figure shows an algorithm that prints the first 10 natural numbers.