Expressions
- Represent a single data item (e.g. character, number, etc.)
- May consist of:
- A single entity (a constant, variable, etc.)
- A combination of entities connected by operators (+, -, *, / and so on)
Examples
Statements
- Cause an action to be carried out
- Three kinds of statements in C:
- Expression Statements
- Compound Statements
- Control Statements
Expression Statements
- An expression followed by a semi-colon
- Execution of the statement causes the expression to be evaluated
Examples
Line 1 is an assignment statement. It assigns the value 0 to the variable i.
Line 3 is an incrementing statement. It increments the value of i by 1.
Line 5 is an assignment statement. It first evaluates the expression 5 + i and assigns the results to the variable a.
Line 7 is yet another assignment statement, though a bit more complex. The result of m multiplied by x is added to b and the final result is assigned to y.
The statement on line 9 causes the printf function to be evaluated. For example, if the value of m is 5, the result would be "Slope = 5" in the UART1 window.
Line 11 is an empty statement. It does nothing and is often referred to as a null statement. It makes little sense by itself but is very useful in other circumstances which we will see later on. Note that this is NOT equivalent to a nop instruction. This generates no code at all.
Compound Statements
- A group of individual statements enclosed within a pair of curly braces { and }
- Individual statements within may be any statement type, including compound
- Allows statements to be embedded within other statements
- Does NOT end with a semicolon after }
- These are also called 'Block Statements'
Example
Control Statements
- Used for loops, branches, and logical tests
- Often require other statements embedded within them
Example
This statement contains a compound statement which consists of two expression statements. The compound statement will continue to run as long as the value of distance doesn't exceed 400.0.