Jun 3, 2019 3 min read

Building Blocks of Programming Languages

Table of Contents

Notes on the basic programming building blocks: expressions, statements, statement blocks, and function blocks.

  • Four basic building blocks of programming languages:
  1. Expressions
  2. Statements
  3. Statement Blocks
  4. Function Blocks

1. Expressions

  • Expressions in computer programming have the same definition as expressions in math: they are a combination of an operator and its operand(s). In keeping with the mathematical definition of an expression, they are well-defined, meaning that an expression must ultimately resolve to a value.
  • An operator tells the computer to perform some kind of mathematical or logical manipulation and is performed on one or more operands
  • Examples:
  • a + b
  • + is the operator; a and b are operands
  • x - 2
  • - is the operator; a and b are the operands
  • a < b
  • < is the operator; a and b are the operands
  • With the above examples, two operands are in play, which is why you'll hear them referred to as binary expressions
  • As a result, binary expressions use binary operators; binary operators operate on two operands
  • Operator Classification:
  • Operators can be classified based on the number of operands they perform their operation on:
  • Unary Operators
  • Take one operand
  • Example: & (address-of operator); see Pointer tutorial
  • Binary Operators
  • Operate on two operands and are by far the most common
  • Examples: +, -, <, =, etc.
  • Ternary Operators
  • Operate on three operands
  • Operators can also be classified based on the kind of function they perform:
  • Arithmetic (math) Operators:
  • i.e. Operators that perform math
  • Examples: +, -, /
  • Relational Operators:
  • Compare the values of two operands
  • Examples: >, <, ==
  • Return/resolve to a boolean: true (1) or false(0)
  • Logical Operators:
  • Combine logical expressions
  • Examples: && (AND), || (OR)
  • Return/resolve to a boolean: true; false

2. Statements:

  • Statements are syntactically complete instructions
  • In C, syntax dictates that all statements end with a semicolon (this semicolon is known as a statement terminator)
  • Example:
  • Variable assignment:
  • a = 4;
  • This is a syntactically complete instruction; note how it is simply an expression (a = 4) consisting of the assignment operator (=) with the operands a and 4, and terminating with a semicolon as required by C. By syntactically correct, we simply mean that the instruction complies with the rules of the language (i.e. syntax).

3. Statement Blocks:

  • Statement blocks group statements together so they act like a single statement (i.e. the statements act together as a block)
  • In C, statement blocks start with { and end with }. In Python, statement blocks are controlled by indentation. This is why whitespace matters in Python but not in C.
  • The code inside the statement block is known as the statement block body
  • Example:

4. Function Blocks:

  • Function blocks are blocks of code that accomplish a single task
  • Functions allow you to reuse code so that if you need to do the same thing multiple times, you simply call the function tag wherever you need it; you separately define the function (with its function block) elsewhere in your code
  • This makes it easier to maintain your code since, if you need to update your function, you only have to update the code once in the function itself and not multiple times wherever the function was called
  • Functions also help when working on big projects with multiple developers by acting as a black box
  • Good functions act like a black box in the sense that you don't have to waste your time, brainpower, or memory knowing exactly how the code inside the function (it's function block) works; you just have to know that for a given input, you get a given output
  • This is also the basis of the idea behind libraries- you don't have to know exactly how something is done. You just call the function (written by someone else) from the library; this forms the very basis of abstraction which allows for collaboration
  • Functions also help for code readability; instead of mentally having to parse out multiple lines of code you can look at the function name (like verifyPhoneNumber() ) and know that it verifies the phone number.
  • Example:
  • int addNum(int a, int b){//Function block here }
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to The Engineer's Workshop.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.