Syntax Structures Flashcards
Syntax Structures
pattern of symbols that make up a code form
Code form
the form of an
if-statement or a while-loop
How to read these code forms
- Anything in ALLCAPS is meant as a replacement spot or hole.
- Seeing [ALLCAPS] means that part is optional.
if-statement
basic logic branching control
if(TEST) { CODE; } else if(TEST) { CODE; } else { CODE; }
switch-statement
like an if-statement but works on simple integer constants
switch (OPERAND) { case CONSTANT: CODE; break;default: CODE; }
while-loop
your most basic loop
while(TEST) {
CODE;
}
while-continue-loop
while(TEST) { if(OTHER_TEST) { continue; } CODE; }
while-break-loop
use break to exit a loop
while(TEST) { if(OTHER_TEST) { break; } CODE; }
do-while-loop
an inverted version of a while-loop
runs the code
then tests to see if it should run again
do {
CODE;
} while(TEST);
Can have continue and break inside to control how it operates
for-loop
controlled counted loop through a fixed number of iterations using a counter
for(INIT; TEST; POST) {
CODE;
}
enum
enum { CONST1, CONST2, CONST3 } NAME;
goto loop
A goto will jump to a label
Used for error detection and exiting
if(ERROR_TEST) { goto fail; } fail: CODE;
Define a function
TYPE NAME(ARG1, ARG2, ..) { CODE; return VALUE; }
or
int name(arg1, arg2) { CODE; return 0;
typedef
defines a new type
typedef DEFINITION IDENTIFIER;
or
typedef unsigned char byte;
Don’t let the spaces fool you; the DEFINITION is unsigned char and the IDENTIFIER is
byte in that example.
struct
A struct is a packaging of many base data types into a single concept
struct NAME {
ELEMENTS;
} [VARIABLE_NAME];
The [VARIABLE_NAME] is optional
or
typedef struct [STRUCT_NAME] {
ELEMENTS;
} IDENTIFIER;