Wednesday, April 20, 2011

Control Statements

Introduction:

Microcontrollers are usually used to perform tasks that have or display intelligence.  The key to intelligence lies in the ability to make decisions and then act on these decisions.  Control statements are used in the C language to make decisions and control the flow of the program based on the results of these decisions.

Program Blocks and Loops:
A program block is a group of statements that have the following two characteristics:  They have a single entry point and a single exit point.  A loop has a program block at its heart.  A loop is used to repeatedly perform an operation or a block of code through the use of a conditional expression.  (Code blocks were covered in more detailed during the discussion of Program Structure.  Click here to re-visit that discussion.)
The conditional expressions were covered in depth under the C operators section.  The important concept to remember is that a false condition is define as a value of zero and a true condition is defined as NOT false.  (Click here to re-visit the discussion on relational operators)
The proper use of braces and indentations are important to maintain readable code.

 Control Structures
The C language control statements include the if/else, the do/while, the while, the for loop, and the switch/case.  The nature of the loop or control structure used determines the number of times the statements (or code block) are executed, if executed at all.  This section will introduce and apply the control statements to perform useful tasks that add "intelligence" to the software.  With the above listed control statements, a block of code can be executed once, executed repeatedly, executed a certain number of times, or skipped over completely.  These control statements are key in writing structured software.  Nesting of control structures also allows for more detailed and complex software.
Structured programming is a technique for organizing and coding programs that reduces complexity, improves clarity, and facilitates debugging and modifying.  The benefits are listed below:
  1. Operation is simple to trace
  2. Structures are self-documenting
  3. Structures are easy to define in flowcharts
  4. Structured programming increases programmer productivity
  5. Structures lead to functions
Number 3 above refers to flowcharts.  Flowcharts are graphical representations of algorithms.  A properly drawn flowchart is an invaluable tool for identifying the proper control structure.

IF / Else:

The IF/ELSE  statement can be referred to as a "fork in the road".  This statement directs the flow of the software according to the result of the conditional expression.  The conditional expression in an if statement is a comparison between two values.  If the conditional expression is true, then the "true" block of code is executed.  If the conditional expression is evaluated as false, then the else branch or block of code is executed.  It should be noted that not having the microcontroller perform any task in the event that the conditional is false is quite common.  Therefore, the else condition may be omitted altogether. 
The flowchart for both an IF and an IF / ELSE structure are shown below.  The distinguishing characteristic that identifies an IF statement over the other control structures is the flow of the program after the decision and resulting process.  In either an IF or IF / ELSE structure, the program flow continues to go down the page. 


if (the conditional expression is true)
     {

     perform these statements
     }
else
     {

     perform these statements if the conditional is false
     }

While Loop:
In a While Loop, the expression is evaluated when the program enters the top of the loop.  If the expression is true, the statements within the loop are executed.  When the bottom of the code block is reached, program flow is directed back to the top of the loop and the conditional is re-evaluated.  As long as the conditional is evaluated as true, the process repeats. IF the conditional is evaluated as false, then the code block is skipped.  Again, the flowchart for a While Loop is shown below.


 while (some conditional expression is true)
      {

      perform the statements located between the braces
      }

DO / While:
The Do/While loop is very similar to the while loop.  The major difference is that the expression or conditional is tested at the bottom of the loop.  This means that the body of the loop is executed at least one time regardless of the results of the conditional.  If the result of the conditional is true, then the loop body is repeated.  This process is repeated until the conditional is evaluated as false.


do
     {

     perform the statements located between the braces
     } while (this conditional expression is true)


The while and do/while loops are frequently used to monitor a particular input or register in control type applications.

For Loop:
The For Loop is traditionally used to perform a task or repetitive event for a known number of iterations or in other words, it is used to implement count-controlled loops.  The for loop is made up of an initial condition, a conditional expression, a modifier, and the body or the code block.  When the for loop is encountered, the initial condition is executed.  The conditional expression is then evaluated as either true or false.  If the conditional is determined to be true, then the body of the loop is executed.  Upon reaching the bottom of the loop, the modifier is executed.  Program control is then returned to the conditional.  The conditional expression is re-evaluated, and if true, the body of the loop is executed again.  Each time the bottom of the code block is reached, the modifier is executed.  The loop continues to be executed until the conditional is evaluated as no longer true.
The flowchart for a For Loop is shown below.  The basic shape of the flowchart resembles that of a While Loop.  The difference or characteristic that determines that it is a For Loop is that the initial condition, conditional, and modifier all refer to the same variable.

 
for ( variable initialized ; conditional expression ; variable modifier )
     {
     perform these statements while the conditional expression is true
     }

The resemblance or similarities between the For Loop and While Loop go beyond the fact that the flowcharts look similar.  In actuality, any For Loop can be replaced or implemented with a While Loop.  However, not all While Loops can be replaced by a For Loop.

Switch / Case
The Switch Case structure is similar to a host of if statements.  The switch case structure may have a default condition that is executed if none of the statements match.  A switch/case structure is used when one statement must be chosen from many.

switch ( variable of interest )
    {
    case    value1:
                                code to be executed.
                                break;
    case    value2:
                                code to be executed.
                                break;
    case    value3:
                                code to be executed.
                                break;
    .
    .
    .
    default:
                                code to be executed.
                               
break;
    }


Choosing the proper control statements:
Picking the appropriate control structure will ensure the proper operation of the software and aid in the readability of the software.  The use of a properly developed flowchart can aid in the selection of the control structure.  Each control structure has its own unique program flow which is evident in a flowchart.  Flowcharts can now be used as a tool to translate an algorithm into C code.
Control structures have the common element of a decision located at some point in the flowchart.  The location of the processes associated with the decision aid in the identification of the structure.  In other words, the relationship of the processes and decision determine the type of control statement.  Another key element in the identification of the control structure is the flow through the processes.  The While, DO / While, and For Loops all have the flow of the algorithm going back towards the top of the flowchart.  The IF / Else and Case / Switch structures both have their program flow going towards the bottom of the flowchart.

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

Enhancing Unix Proficiency: A Deeper Look at the 'Sleep' Command and Signals

Hashtags: #Unix #SleepCommand #Signals #UnixTutorial #ProcessManagement In the world of Unix commands, there are often tools that, at first ...