Structured Program Development in C
Tags: c, programming, tutorial, computerscience
Published: 2021-08-26 16:08:43

### 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

1. The actions to be executed.
1. 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:

1. Get out of bed
1. Take off pajamas
1. Take a shower
1. Get dressed
1. Eat breakfast
1. 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:

1. Get out of bed
1. Take off pajamas
1. Get dressed
1. Take a shower
1. Eat breakfast
1. 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**.<br/>
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**.<br/>

All programs could be written in terms of only three control structures:

1. Sequence structure
1. Selection structure
1. Iteration structure

The **Sequence Structure** is simple:<br/>
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.

1. The *if* selection statement either selects (performs) an action if a condition is true or skips the action if the condition is false.
1. The *if … else* selection statement performs an action if a condition is true and performs a different action if the condition is false.
1. 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:

1. *while*
1. *do … while*
1. *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.<br/>
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.*