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