Logic

Logic is the study of reasoning and when we add this to the control structures, they become algorithms.

Have you ever watered the plants using a water pipe or washed a car with it? How do you think it works?

The pipe guides the water from the water tap to the car. It makes sure optimum amount of water reaches the end of the pipe. A pipe is a control structure for water in this case. We will understand more about control structures in the next topic.

How does a control structure work?

Everything that we see around us, has a name. In a class, there are several students. When the teacher says I will give the prize to the best student. The student could be anyone in the class – Eric, Emily, Jonnathan, and so on.

We conclude, that the word student, is a variable.

Similarly, while starting to write code, we define a box, which can contain different values at different times, depending on the condition that we define. A control structure, defines the final value of the variables.

A precondition is the state of a variable before entering a control structure. In the gift wrapping example, the size of the gift determines the amount of gift wrapping paper you will use. Hence, it is a condition that you need to follow to successfully finish the task. In programming terms, such condition is called precondition. Similarly, a post condition is the state of the variable after exiting the control structure. And a variable, in code, is an alphabetic character, or a set of alphabetic characters, representing or storing a number, or a value. Some examples of variables are x, y, z, a, b, c, kitten, dog, robot

Let us analyze flow control by using traffic flow as a model. A vehicle is arriving at an intersection. Thus, the precondition is the vehicle is in motion. Suppose the traffic light at the intersection is red. The control structure must determine the proper course of action to assign to the vehicle.

How does a control structure work?

Thus, upon exiting the control structure, the vehicle is stopped.

If you wonder where you learnt to wrap the gift, you would know that you learnt it by observing other people doing a similar task through your eyes. Since our microcontroller does not have eyes, we need to teach it to have a logical thinking using code.

The series of logical steps that lead to a solution is called algorithm as we saw in the previous task. Hence, all the instructions we give to a micro controller are in the form of an algorithm. A good algorithm solves the problem in a fast and efficient way.

Blocks of small algorithms form larger algorithms. But algorithm is just code! What will happen when you try to add sensors to your code? A combination of electronics and code can be called a system.

How does a control structure work?

Logic is universal. Just like there can be multiple ways to fold the wrapping paper, there can be multiple ways to solve a problem too!

A micro controller takes the instructions only in certain languages. The instructions then go to a compiler that translates the code that we have written to the machine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset