Syntax versus Semantics

Context: FIT2102_MOC · the distinction that makes the whole unit transferable — Programming Paradigms are differences of semantics, and language choice is mostly syntax · the ladder those semantics sit on is Levels of Abstraction (Machine to High-Level)

Quick Revision

  • 🎯 Objective: syntax = which symbol combinations are well-formed; semantics = what the machine does when it runs them ➔ two programs can differ entirely in syntax and be identical in semantics.
  • ⚡ Key Constraint: learn the semantics; the syntax is the disposable part. FIT2102 studies few languages on purpose — concepts transfer, keywords do not.

📝 Core

  • Syntax ➔ the set of rules defining which combinations of symbols count as correctly structured statements or expressions in a language. A syntax error means the text was never a program.
  • Semantics ➔ the processes a computer follows when executing a program in a given language. A semantic difference means the machine does something else.
  • Same semantics, different syntaxsumTo in Python (while i < n:, indentation-delimited) and in JavaScript (for(let i = 0; i < n; i++), brace-delimited) compute the identical result: the syntax is unrecognisably different, the semantics are the same.
  • Same semantics, different syntax — within one languagewhile vs for, if/else vs the ternary x >= y ? x : y. Choosing between them is a readability decision, not a behavioural one.
  • Why this pays ➔ a concept learned as semantics is portable to any language that offers the abstraction; a concept learned as syntax dies with the language.

🧬 Evaluation Model

The same computation, three surfaces — all three reduce to “accumulate :

SurfaceCodeSemantics
Python whilesum = 0; i = 0 · while i < n: sum += i; i += 1mutate two variables until the guard fails
Python forfor i in range(0,n): sum = sum + isame mutation, loop variable managed for you
JavaScript forlet sum = 0; · for(let i = 0; i < n; i++) { sum += i; }same mutation, different delimiters
  • Desugaringx >= y ? x : yif (x >= y) { return x } else { return y }; the ternary is an expression (it has a value), the if is a statement (it does something). That is a real semantic difference hiding inside apparent sugar.

⚠️ Common Mistakes

  • 💡 Calling a semantic difference “just syntax” ➔ a statement and an expression are not interchangeable: only the expression form can be passed, returned, or composed.
  • 💡 Assuming range(0,n) and i < n agree by luck ➔ both exclude , which is why the two sumTo versions match. Change either bound and the semantics diverge while the syntax looks fine.

🧠 Active Recall