Expressions and Statements

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.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.