This is the 2nd chapter of Embedded programming tutorial. For other chapters please go to the links provided in the last portion of this page here we discuss about use of statements in 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).
- 1 Use of Statements in programming :
- 2 Key point : Application of various statements (where to use statements)
Use of Statements in programming :
C programming is a combination of statements. Statements are various types they are expression statement, repetition statement, control execution & selection statement and special control statements are the types of statement used by C language.
In previous chapter we have seen examples of expression statements which are used to assign values to the variables and they are used to make a call to function. This chapter will explain you repetition statements in our day to day life. We use machines to do the repetitive tasks.
Day to day life example of repetition statements :
For example we set alarm clock once and it rings every morning automatically. This makes our life easier and we can spare time for our main daily activities similarly in C language we use repetitive statements. If I ask you to write a simple program to calculate sixth power of five then your program without the usage of repetitive statements may look like as shown in the below image.
Look at the C program source code, it is very lengthy code see this below image to see how repetition statements can reduce the program size.
The modified code is highlighted by a blue colour. We have used a ‘FOR’ statement to simplify our task of calculation. FOR is used to create a loop. The loop will run for a certain number of times as specified in the first statement.
FOR statement has three sections (please check above image too), inside the round bracket. These section are separated by a semicolon (why we use semicolon in programming, click here). Variable is used inside the first statement as a counter which defines the for loops execution. Here in the first section inside the round bracket we have assigned value 0 to variable i. This section inside the for loop is reserved to assign value to the counter variable. You can use any value to initiate a counter based on your requirements.
The second section defines the maximum limit of the counter variable, this way execution cycle of the FOR loop is defined here we have specified I variable to remain below 6. Thus the loop will execute while the I value is less than 6. As we have started the execution of FOR loop with base value of I is 0 and ending it with value equal 5, thus the loop will execute for 6 times.
The third section in FOR loop declaration is used to increment or decrement the values of counter variable. Here I will get increment by one. This is denoted by sign ++. The sign ++ is a shortcut to writing I = I+1. You can also use minus sign to decrement the variable. But don’t use it in this program as it is not suitable as we have defined a FOR loop to execute 6 times the calculation statement will also get executed 6 times. This statement will multiply number 5 for 6 times. Same thing can be done by other repetitive statements like WHILE loop and DO-WHILE loop.
WHILE and DO-WHILE statements
Lets move on to next image to see details of WHILE and DO-WHILE statements.
Refer the source code changed. Source code is indicated by blue colour. WHILE loop is based on a condition. Read the code written inside the round bracket. You have to write condition statement inside the bracket. Here in this example we have specified that the value of variable I should be less than 6. Remember you have to initialize the value of I before program enters into the loop we have initialized it is 0. Considering the initialization value in the condition statement of while loop. It is simple to say that the loop will run 6 times the same program can be developed using a DO-WHILE loop.
Lets move to the next image. This example demonstrates DO-WHILE loop.
The main difference between DO-WHILE loop and other two loops, we have seen earlier is DO-WHILE loop guarantees at least one execution . This is because the condition for the looping statement is checked. At the end you have to write the condition for the loop inside the round brackets after WHILE as shown. DO-WHILE loop terminates one condition is not satisfied. In other words value of I no more remains less than 6 loop will terminates.
Key point : Application of various statements (where to use statements)
Lets move on to the applications of various loops and when to use which type of a loop we have solved our problem statement of calculating sixth power of five using three different loops. But which is more suitable for us. FOR loops are generally when you know exact number of iterations. Here an example of a simple FOR loop a while loop is basically a simple FOR loop with no initialization or update section. DO-WHILE loop are essentially same as the WHILE loops except that you know they all be executed at least once. Thus use FOR loops for the repetitive tasks whose number of iterations are known before writing a source code. Use WHILE loops for a task which asks user to enter the value for the number of iterations.
For example if we modify our program to calculate the nth power of five by asking value of n to the user. We should use WHILE loop as we can substitute the condition statement as a less than DO-WHILE loop. When we want to execute the set of statements at least once and then check the condition statement. By now it must be clear to you that FOR loop is the best choice to solve our problem by calculating 6 th power of 5. Also you learned about the use of Statements in programming.
Now I am winding up chapter 2, Click here for Chapter 3