Program Flow In Embedded C
Program Flow and control is a control method of your program. For example Loop constructions control repeated execution of repeated program segments where control is taken by control parameter. In this article we will go through if/else switch/case statements and loop sentences like while, do while, for.
As I mentioned three looping sentences available in C language one of them is While sentence. Lets take an example:
int guess, i;
printf(“Square root of 10000 is %d\n”, guess);
While(guess!=i) invokes looping operation. This causes statement to be executed repeatedly at the beginning the condition guess!=i is checked. As long the argument is TRUE the while sentence will be continued continuously. When guess becomes equal to i while statement will be skipped. I am not going to deep in to it as there are tons of information about basic C.
As we didn't know how many times the loop should be executed then we used while sentence, but when we know exactly hof many times we need to execute a sentence, there is another loop sentence for(s1;s2;s3). For construct takes 3 arguments each separated by semicolons. Without any theories lets see an example:
for (i = 0; i < 5; i++)
printf("value of i");
The for loop has four components; three are given in parentheses and one in the loop body.
All three components between the parentheses are optional.
The initialization part is executed first and only once.
The condition is evaluated before the loop body is executed. If the condition is false then the loop body is not executed.
The update part is executed only after the loop body is executed and is generally used for updating the loop variables.
The absence of a condition is taken as true.
It is the responsibility of the programmer to make sure the condition is false after certain iterations.
Do While sentence
do while is somehow similar to while just one difference that there testing of conditions is done after execution of first loop:
printf(" the value of i is %d\n", i);
i = i + 1;
If Else Statement
Lets consider a fragment of program:
I bet there is no need of explanation. Simply if first part true then first sentence is executed else other sentence is executed.
Other case is with if else if sentence structure:
if (condition 1)
simple or compound statement // s1
else if (condition 2)
simple or compound statement // s2
else if ( condition 3)
simple or compound statement // s3
else if ( conditon n )
simple or compound statement // sn
You can use if-else if when you want to check several conditions but still execute one statement.
When writing an if-else if statement, be careful to associate your else statement to the appropriate if statement.
You must have parentheses around the condition.
You must have a semicolon or right brace before the else statement.
case constant expressions
case 0: printf ("Number less than 10"); // A
case 1: printf ("Number less than 20"); // B
case 2: printf ("Number less than 30"); // C
default: printf ("Number greater than or equal to 40"); // D
The switch expression should be an integer expression and, when evaluated, it must have an integer value.
The case constant expression must represent a particular integer value and no two case expressions should have the same value.
The value of the switch expression is compared with the case constant expression in the order specified, that is, from the top down.
In the absence of a break statement, all statements that are followed by matched cases are executed. So, if you don't include a break statement and the number is 5, then all the statements A, B, C, and D are executed.
If there is no matched case then the default is executed. You can have either zero or one default statement.
In the case of a nested switch statement, the break statements break the inner switch statement.
The switch statement is preferable to multiple if statements.