DEV Community

Ahmed Gouda
Ahmed Gouda

Posted on • Edited on • Originally published at ahmedgouda.hashnode.dev

Structured Program Development in C

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.
  2. 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
  2. Take off pajamas
  3. Take a shower
  4. Get dressed
  5. Eat breakfast
  6. 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
  2. Take off pajamas
  3. Get dressed
  4. Take a shower
  5. Eat breakfast
  6. 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:

  1. Sequence structure
  2. Selection structure
  3. 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.

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