Reader small image

You're reading from  Learning C for Arduino

Product typeBook
Published inMar 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781787120099
Edition1st Edition
Languages
Right arrow
Author (1)
Syed Omar Faruk Towaha
Syed Omar Faruk Towaha
author image
Syed Omar Faruk Towaha

Syed Omar Faruk Towaha is a data scientist, and currently working at Meta. He has two bachelor's degrees, one in Physics, another in Computer Science and a Master's in Data Science. He is based in London, England, and has interests in data science and IoTs. While Syed is not busy with his jobs, he uses his time to write something about new technologies. His recent publications include Building Smart Drones With ESP8266 and Arduino, Introduction to Rust Programming, Learning C for Arduino, and JavaScript Projects for Kids.
Read more about Syed Omar Faruk Towaha

Right arrow

Chapter 4. Blinking with Operations and Loops

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.

Expression in C


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.

Tip

Remember, we use double equal signs (==), not a single equal sign (=) to indicate a data type is equal to another. The preceding expressions only work when we use logical conditions in C.

Logical operations in C

Sometimes, an event may depend on something. Say, if the weather is good, I will...

Logical operators


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

AND

&&

This means, if all the conditions are true, then the condition is true.

OR

||

This means, if any of the conditions are true, then the condition is true.

NOT

!

This operator reverses the condition.

Now we will learn about the operators in detail.

AND operator

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...

Exercises


  • 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!"

Loops


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 loop

  • while loop

  • do-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.

for loop

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) 
{ 
 ...

Exercise


Using a loop, draw the following shapes:

a.

*
* *
* * *
* * * *
* * * * *
* * * * * *

b.

* * * * * *
* * * * *
* * * *
* * *
* *
*

c.

11
1111
111111
11111111

Summary


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.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning C for Arduino
Published in: Mar 2017Publisher: PacktISBN-13: 9781787120099
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Syed Omar Faruk Towaha

Syed Omar Faruk Towaha is a data scientist, and currently working at Meta. He has two bachelor's degrees, one in Physics, another in Computer Science and a Master's in Data Science. He is based in London, England, and has interests in data science and IoTs. While Syed is not busy with his jobs, he uses his time to write something about new technologies. His recent publications include Building Smart Drones With ESP8266 and Arduino, Introduction to Rust Programming, Learning C for Arduino, and JavaScript Projects for Kids.
Read more about Syed Omar Faruk Towaha