Arduino 平台与C语言程序设计-week2-C Programming-Lesson2

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.

2. Lesson 2

2.1 Lecture2-1: Basic COperators

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.

2.1.1 Constants

(1) Can use #definecompiler directive

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

(2) Any instance of the string is substituted before thecompile time

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.

(3) Character constants

  • Witten as a single character insingle quotes
  • #defineTERMINA ‘x’
  • Integer equal to the ASCII value of the character
  • Some characters are not easy to represent (i.e. bell –some ring sound from the machine)

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.

2.1.2 Arithmetic/Relation Operators

  • Arithmetic Operators: +, -, *, /
  • % is the modulo operation,division remainder
  • Ex. 9%2=1, 9%3=0
  • ++(increment), --(decrement)
  • Relation Operators: ==, <,>, <=, >=, !=
  • Ex. If(x < 5)

2.1.3 Logical Operators

  • && (AND), || (OR), !(NOT)
  • Treat argument as 1-bit binary values

0 is FALSE, not-0 is TURE

  • If ((A==1) && !B)

Logical operators work. They take the arguments and treat them as either true or false, just like a 1-bit binary value.

2.2 Lecture2-2: Conditionals

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.

2.2.1 Conditional Statements

(1) if esle

  • else is optional
  • expression is evaluated

Execute statement1 if TURE, statement2 if FALSE

  • expr2 evaluated if expr1 is FALSE

(2) Switch

  • expression is evaluated, compared to const_expr
  • Statements are executed corresponding to the first matching expression
  • default is optional

(3) Break in a Switch

  • Without abreak statement, the case will not end
  •  if x== 0 then both y = 1; and y = 2; are executed

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.

2.3 Lecture2-3: Loops

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.

2.3.1 While and For Loops

  • Initialization and increment are build into the for loop
  • Condition checked at the top of a for/ while loop
  • Condition checked at the bottom of a do-while loop

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.

(1) Initialization step: expr1

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.

(2) Conditional check: expr2

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.

(3) Increment step: expr3

Increase some variable to get close to the check condition.

2.3.2 Loops examples

while loop


for loop

2.3.3 Break and Continue

  • Break jumps to the end of a for, while, do. or case
  • Continue jumps to the next iteration of a loop


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值