If I say a circle looks like a rectangle, would you oppose my statement? Yes, you would. Why? Because a circle is a round-shaped object, whereas the rectangle has four angles. In your brain, you have images of both circles and rectangles. You just compared a circle to a rectangle and opposed my statement. This is how logic works. We compare with something and decide if our statement is true or false. In this chapter, we are going to learn about logical operations with Arduino IDE. We will also learn about loops.
You're reading from Learning C for Arduino
Before we learn about logical operations, let's look at a few expressions in C programming. Suppose you need to identify the larger of two numbers (100 and 200). You will definitely say 200 is larger than 100, right? The question is, how did you get your answer? Yes, by comparing the numbers with each other. The condition is 100<200, or 200>100. The two symbols (< and >) are known as less than and greater than. Let's look at a few more expressions:
Expression |
Meaning |
x == y |
x is equal to y. |
x != y |
x is not equal to y. |
x < y |
x is less than y. |
x > y |
x is greater than y. |
x <= y |
x is less than or equal to y. |
x >= y |
x is greater than or equal to y. |
Before going any further, let's learn about a few logical operators. There are only three types of logical operators in C. They are, AND
, OR,
and NOT.
All three operators cover any logical operation. These operators help to remove complexities and shorten the code. Let's learn their symbols first:
Name |
Symbol |
Meaning |
|
|
This means, if all the conditions are true, then the condition is true. |
|
|
This means, if any of the conditions are true, then the condition is true. |
|
|
This operator reverses the condition. |
Now we will learn about the operators in detail.
The AND
operator means if all the conditions fulfill the condition, the condition is true.
Let's look at an example.
Suppose, if today is Monday and the weather is good, I will go to school. See, if both conditions is true, I will do something. But if any of the conditions are false, I won't go to school. In this case, we can either use a nested if or a logical operator...
Write a program to check if a number is odd or even (hint: find the modulus of the number by 2, and if the modulus is equal to 1, then the number is even; else the number is odd).
Take inputs of three variables (Name, Age, and Email) from a user. If the age is greater than 20, print all the variables. If the age is equal to or less than 20, print "Sorry! You are not allowed!".
Take an input of three integers and print the largest integer (hint: follow the previous example)
Take the input of the marks of a student (four subjects) and find his GPA (hint: take variables as float; use if conditions to find the GPA of a subject; then add all the GPAs, divide the total with the total number of subjects, and print the value).
Define the days of a week from 1 to 7. Take an input of the day number, and if the day number is odd, print "Have a lovely day;" if the day number is even, print "It is a lovely day!"
Loop executes a sequence of statements until a specific condition is true. Loops help to do things again and again according to the conditions. In programming, we use loops if we need to do the same thing multiple times. There are basically three types of loops in C for Arduino. They are as follows:
for
loopwhile
loopdo-while
loop
Each loop has its own structure, but one thing is common to all of them: each loop needs a condition to control the loop. Let's look at the for loop first.
The for
loop has four major parts. They are as follows:
Initialization
Condition
Increment/Decrement
Statements
The initialization means assigning initial values to a variable. The condition controls the loop in terms of how many times the loop will run. The increment/decrements operator means increasing, decreasing, or changing the value of the variable until it matches the condition. The basic syntax of a for loop is as follows:
for (initialization; conditions; increment/decrement) { ...
In this chapter, we have learned about a lot of things, including control statements (if-else, loops, switch, and so on). All the control statements we learned are equally important for the next level of Arduino programming. This chapter might be a long one, but my suggestion is that you read the whole chapter without stopping. You should also do the exercises so that your learning remains in your brain for a long time. If you have any problems understanding any code or topic, then you will not be quite ready for the next chapter, where we will learn a lot of interesting things, including file handling and functions. If you are already familiar with all the topics of this chapter, you are most welcome to move onto the next Chapter 5, Functions and Files with Arduino.