Algorithms
The solution to any computing problem involves executing a series of actions in a specific order. A procedure for solving a problem in terms of
- The actions to be executed.
- The order in which these actions are to be executed.
is called an algorithm.
The following example demonstrates that correctly specifying the order in which the actions are to be executed is important. Consider the rise-and-shine algorithm followed by one junior executive for getting out of
bed and going to work:
- Get out of bed
- Take off pajamas
- Take a shower
- Get dressed
- Eat breakfast
- Carpool to work
This routine gets the executive to work well prepared to make critical decisions. Suppose that the same steps are performed in a slightly different order:
- Get out of bed
- Take off pajamas
- Get dressed
- Take a shower
- Eat breakfast
- Carpool to work
In this case, our junior executive shows up for work soaking wet.
Specifying the order in which statements are to be executed in a computer program is called program control.
Control Structures
Normally, statements in a program are executed one after the other in the order in which they’re written. This is called sequential execution.
Various C statements enable you to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control.
All programs could be written in terms of only three control structures:
- Sequence structure
- Selection structure
- Iteration structure
The Sequence Structure is simple:
Unless directed otherwise, the computer executes C statements one after the other in the order in which they’re written.
Selection Statements in C
C provides three types of selection structures in the form of statements.
- The if selection statement either selects (performs) an action if a condition is true or skips the action if the condition is false.
- The if … else selection statement performs an action if a condition is true and performs a different action if the condition is false.
- The switch selection statement performs one of many different actions, depending on the value of an expression.
The if statement is called a single-selection statement because it selects or ignores a single action. The if…else statement is called a double-selection statement because it selects between two different actions. The switch statement is called a multiple-selection statement because it selects among many different actions.
Iteration Statements in C
C provides three types of iteration structures in the form of statements:
- while
- do … while
- for
So C has only seven control statements:
- Sequence
- Three types of selection
- Three types of iteration
Each C program is formed by combining as many of each type of control statement as is appropriate for the algorithm the program implements.
Thus, any C program we’ll ever need to build can be constructed from only seven different types of control statements combined in only two ways Stacking and Nesting. This is the essence of simplicity.
Top comments (0)