This is the 3rd chapter of Embedded programming tutorial. For other chapters please go to the links provided in the last portion of this page. This post deals with conditional, execution and selection statements in Embedded programming.
If you are interested to do Machine learning (Embedded system) course through online, with certification and proper placement log on to Udacity (use this link for sign up, thus you get 1000 rs discount in your first enrolment).
Till now you are aware that C language expression statements and repetitive statements are used. These statements are used to assign values and to execute repetitive tasks. Any programming language will be incomplete if it does not have a decision making statements. These decision-making statements are also called as conditional, execution and selection statements.
- 1 Realistic approach to a sample program
- 2 SWITCH Statement
- 3 IF Statement
- 4 Use of ampersand in program
- 5 Logical operators in C program
- 6 IF statement
Realistic approach to a sample program
let’s take a simple task this time we will be more realistic in our sample program our task is to develop a remote control for TV This remote control has 20 buttons on it to execute various tasks like channel scan, channel switching, volume level changes etc. Here our task has defined set of inputs we have 20 different inputs and we have to process the respective commands to perform the requested task.
let’s move on to an example in the below image.
This example uses a SWITCH statement.
SWITCH statement is used to select user input and call respective function. we have not actually called any function here but simply added comments after slash slash text. after slash slash is not a part of the program. you can write any documentation part after slash slash. imagine that get user key is a function written somewhere else and is used to accept key from the remote control this function tells us as which key is pressed. This key value is stored in number variable later the variable number is provided as input to the SWITCH statement. SWITCH statement has predefined options from 0 to 2. I have omitted rest of the numbers to fit the program in one screen if user key as any of 0 to 2 respective case will get executed and rest of them will be ignored. If for some unforeseen situation, if the value of variable is not in the range of 0 to 2 then a default section will be executed.
SWITCH statements are used when your program requires a predefined set of decision making options but if your program requires a range of selection. For example do some tasks if the temperature is in the range of 20 to 30 centigrade you have to use IF statements to solve this problem.
let’s move on to the next image to learn more about IF statements. Look at the below image.
This sample program monitors an input temperature received from get temperature function. we have used IF statement to check the input temperature.
Condition required for IF statement
The IF statement requires a condition. This condition is put inside the round bracket. The first IF statement checks the input temperature is less than 20( Please check the above image too). The second IF statement checks if the temperature is equal to or greater than 20 and it also checks if the temperature is less than or equal to 30. This allows us to specify the range of temperature between 20 to 30. further the third IF statement checks if the input temperature is greater than 30.
Use of ampersand in program
Notice the ampersand, ampersand used in the second IF statement. This is a C language syntax to indicate logical and expression. similarly C language provides facility to express our using two pipe symbols.
Logical operators in C program
Next image explains you about the various logical operators used in C language.
which you can combine in IF statement use logical OR operator to indicate that,
IF statement can execute it’s defined task. if any of the two conditions are met. you can use AND logical operator to indicate that you want both the conditions to satisfy. If any one condition is not satisfied it will not proceed further. if you want to compare two values as shown in the third statement (check above image in IF statement heading). use equal to sign twice. This compares value of 45 with temperature. If temperature is exactly 45 then program will enter inside the IF statement, if the temperature is not equal to 45 then the program will do nothing. The next part is very important that if you want to execute program in both the conditions, that is when temperature is equal to 45 and not equal to 45.
The next image shown below will tell you about this
we have to use else statement if we want to perform a task if temperature is equal to 45 and not equal to 4. This sample code will check the temperature and we’ll compare it with value 45. If the temperature is 45 it will enter inside IF statement. Temperature is not equal to 45 then it will enter into the ELSE statement. This way we can perform various tasks of decision-making using SWITCH statement, IF statement and IF-ELSE statements.
Now I am winding up chapter 3, Click here for next chapter