Clean and clear revised lecture notes for programming in C++ using a microcontroller. Includes many syntax examples. Perfect for those who are new to coding or C++. Useful for engineering projects that may require basic coding knowledge. Sections include: Introduction to Key Terms, Loops, Classes a...
Introduction:
Expressions - anything that has a value
A literal - an expression whose value is known before the program runs. 8 is a literal, it has value 8. X
would not be a literal as its value has not been declared
Float - can represent decimal values
A variable - a labelled storage box for a value. Every variable has a type (what type of data it stores)
4 types of main data.
Integral types - holds integers, available in signed or unsigned. Signed can hold negative
numbers.
o Char - 8bits
o Short - 16 bits
o Int - 16 or 32 bits (32 on mbed)
o Long - 32 or 64 bits (32 on mbed)
o Long long - 64 bits
Floating points - can hold decimal values
Compound types
Assignment - changing the value of a variable
Conditional Execution:
Changes the programs behaviour depending on user input, sensor readings, time of day…
For example
If (x > 4) {
// do something
}
Curly brackets enclose multiple
statements
Conditions in C++:
0 can mean false
Any other value can mean true
If (!x) = will be true if x == 0
Loops:
Loops allow branching backwards in your code.
Count controlled loops - executes a certain number of times
Condition controlled loops - executes while a condition is true
Entry controller loops
, Exit controlled loops
Count controlled loop: (for loop)
Used to print numbers 1-10
Or flash an led 5 times
Condition controlled loop: (while loop)
Running a motor until a switch is pressed
Reading a sensor until its value changes
Flashing an led while a button is held down
Do while loop:
Execute loop body
Evaluate condition
If true loop again
Do {
// compute result
} while (result != required);
This is the only exit controlled loop, less commonly used
Choosing the right loop:
Counting or iterating over a known number of things - for loop
Waiting for a button press or reading data until there is no more data - while loop
Infinite loops:
While (true) {
// loops infinitely
}
Or
While (true);
Code underneath these loops will never run as the loop will never end.
Super loop:
Begins with setup statements then a while loop which loops forever.
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller skycass. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.13. You're not tied to anything after your purchase.