Structure: Further Syntax Flashcards

1
Q

Used to end a statement

A

;(semicolon)

Example

int a = 13;
Tip

Forgetting to end a line in a semicolon will result in a compiler error. The error text may be obvious, and refer to a missing semicolon, or it may not. If an impenetrable or seemingly illogical compiler error comes up, one of the first things to check is a missing semicolon, in the immediate vicinity, preceding the line at which the compiler complained.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Used to start and end several different constructs including functions loops and conditional statements

A

{ } Curly Braces

An opening curly brace “{“ must always be followed by a closing curly brace “}”. This is a condition that is often referred to as the braces being balanced. The Arduino IDE (integrated development environment) includes a convenient feature to check the balance of curly braces. Just select a brace, or even click the insertion point immediately following a brace, and its logical companion will be highlighted.

At present this feature is slightly buggy as the IDE will often find (incorrectly) a brace in text that has been “commented out.”

Beginning programmers, and programmers coming to C from the BASIC language often find using braces confusing or daunting. After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop.

Because the use of the curly brace is so varied, it is good programming practice to type the closing brace immediately after typing the opening brace when inserting a construct which requires curly braces. Then insert some carriage returns between your braces and begin inserting statements. Your braces, and your attitude, will never become unbalanced.

Unbalanced braces can often lead to cryptic, impenetrable compiler errors that can sometimes be hard to track down in a large program. Because of their varied usages, braces are also incredibly important to the syntax of a program and moving a brace one or two lines will often dramatically affect the meaning of a program.

Functions

void myfunction(datatype argument)
{
 statements(s)
}

Loops

while (boolean expression)
{
statement(s)
}

do
{
statement(s)
} while (boolean expression);

for (initialisation; termination condition; incrementing expr)
{
statement(s)
}

Conditional Statements

if (boolean expression)
{
statement(s)
}

else if (boolean expression)
{
statement(s)
}
else
{
statement(s)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

lines in the program that are used to inform yourself or others about the way the program works. They are ignored by the compiler, and not exported to the processor, so they don’t take up any space on the Atmega chip.

A

”//” single line comments

/* */ multi-line comments

Comments only purpose are to help you understand (or remember) how your program works or to inform others how your program works. There are two different ways of marking a line as a comment:

Example

x = 5; // This is a single line comment. Anything after the
 // slashes is a comment to the end of the line

/* this is multiline comment - use it to comment out whole blocks of code

if (gwb == 0){ // single line comment is OK inside a multiline comment
x = 3; /\* but not another multiline comment - this is invalid \*/
}
// don't forget the "closing" comment - they have to be balanced!
\*/

Tip

When experimenting with code, “commenting out” parts of your program is a convenient way to remove lines that may be buggy. This leaves the lines in the code, but turns them into comments, so the compiler just ignores them. This can be especially useful when trying to locate a problem, or when a program refuses to compile and the compiler error is cryptic or unhelpful.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

allows the programmer to give a name to a constant value before the program is compiled. However const is preferred generally

A

define constantName value

#define

Syntax

Note that the # is necessary

Example

#define ledPin 3
// The compiler will replace any mention of ledPin with the value 3 at compile time.

Tip

There are no semicolons after the statement. Doing so will return cryptic errors in the debugging. There are also no equal signs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

is used to include outside libraries in your sketch.

A

include <avr><br></br> <br></br>prog_uint16_t myConstants[] PROGMEM = {0, 21140, 702 , 9128, 0, 25764, 8456,<br></br>0,0,0,0,0,0,0,0,29810,8968,29762,29762,4500};</avr>

#include

This gives the programmer access to a large group of standard C libraries (groups of pre-made functions), and also libraries written especially for Arduino. Note that #include, similar to #define, has no semicolon terminator, and the compiler will yield cryptic error messages if you add one.

Example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly