This series of articles are the study notes of "An Arduino platform and C Programming", by Prof. Harris,Department of Computer Science, University of California, Irvine. This article is the notes of week 2, C Programming, lessen 2.
In this lecture,we'll talk a little bit about the basic c operators. All the operators that youperform. That you'd regularly execute in an instruction that is in C.
#define ANSWER 42
First there are constants. You can define constants, there are actually a couple of ways to define constants, but we'll talk about #define. #define is a compiler directive, again its got a hash (#) sign in front of it so it's a compiler directive.
And #define basically substitutes one string for another. So, like here I say #define answer 42. So what it does is wherever it sees answer in the file, it substitutes that with 42.
So it's just a macro substitution. It says,oh I see the word answer, I'm gonna stick a 42 in instead. It happens before the compile, so the pre-processing step.
You can do this with characters. In that case, it was a number, the number 42. You can do it with character constants, too. In this case, if you want it to be a character, you gotta put the character in single quotes. So #define TERMINATOR 'x'. Notice that I put the x in single quotes. What it does is, if you put it in single quotes it knows that it's a character and it interprets it as a character.
So ASCII and Unicode, what they are I haven't described them. But what they are is every character. Visible characteror an invisible character. Every character you see on your keyboard for instance and lots of other characters you can't. They're all mapped to numbers. So, X might be 41, Y might be 42, and so on. They're all mapped to numbers in a table.
So ASCII is only 8 bits long. Really 7 bits. Unicode is 16 bits. So you're going to have a lot more characters. But ASCII and Unicode overlap. So lowercase x has a certain representation, a certain number. And if you put it in single quotes like that it will know, oh this is meant to be a character, so I will represent it with this ASCII value.
0 is FALSE, not-0 is TURE
Logical operators work. They take the arguments and treat them as either true or false, just like a 1-bit binary value.
This lecture is about conditional operation inside a C program. So if you have a program with no conditionals, that's unusual, but you can have a program with noconditionals, where it just executes line after line. But a lot of time, most of the time in programs, you want to have conditionals where you execute some code under some conditions, and execute other code under other conditions.
Execute statement1 if TURE, statement2 if FALSE
If x does match 0, it'll execute every statement from that point on. So that means in the way that this is written if x = 0 it'll say case 0, it'll match that case. It'll assign y equal to 1. But then notice it'll continue execution. It'll go on to the next statement, y = 2, and execute that also. A very weird and annoying thing about switches in C. Because usually that's not what you want. Usually you want these cases to be mutually exclusive, meaning if x = 0, you execute case 0 and that's that. If x = 1, you execute case 1 and that's that. You like them to be separate. But that's not how C is. So in order to make it that way, which is a very common thing people want to do you use a break statement.
So it's very commonin a switch statement if you have 3 cases like this, case 0, case 1, case 2, you put 3 breaks. That’s a very common way to use abreak inside a switch. So be wary of switches when you use them, because if you don't use the break, then if x matches 0, case 0, then it'll also execute case1 as if it matched that too. And that can be confusing sometimes. Sometimes that's what you want, but just be aware.
In this lecture, we'll talk about looping and loop control constructs. So, looping is, basically, doing something over and over again in your code. Happens all the time. There are many times you wanna go through an array and evaluate it.
Actually, in Arduino that's implicit in an Arduino, as in whenever you're executing you do it over and over and over as long as the Arduino is powered on. So, loops are an important construct, and we'll talk about the different ways to do loops in C.
So generally with any kind of loop, regardless of what kind of loop there is, there's always going to be some kind of an initialization step. And some kind of increment step and some kind of a conditional check.
So generally with any kind of loop, regardless of what kind of loop there is, there's always going to be some kind of an initialization step.
And some kind of increment step and some kind of a conditional check. So, every loop has to have some end condition. Some kind of condition that notes, that tells you that the loop is done. You usually don't wanna loop forever. Actually in Arduino's you do, but we'll getto that next course. But often you don't wanna loop forever. Maybe you only wanted to do this thing 100 times. So, you need to be able to check, haveI done this 100 times? All right, so there's always going to be some sort of a conditional check associated with a loop.
Increase some variable to get close to the check condition.
while loop
for loop