sarkinen.com > johan > Teaching > Programming
3. What Is a Language? |
|
< 2. Why Learn Programming? | 4. What Languages to Learn > |
Learning a formal and constructed language - like a programming language (e.g. JavaScript) or a markup language (e.g. HTML) - is basically no different from learning a natural language like English or Swedish.
You need to learn words and grammars, and consider semantics (the meaning). And practice - practice a lot!
When engineers talk about formal languages, you will more often see syntax instead of grammar1. So, from an engineering and computer science major viewpoint:
Words: the vocabulary the language have, often denoted reserved words in programming languages
Syntax: how words may be put together, the rules for putting words together (~grammar in natural languages) Semantics: the meaning of how the words were put together 1 Note that syntax and grammar are not identical terms, but let's not muddy the waters here with too much unnecessary semantics. (Google difference between grammar and syntax)
As a brief illustration, English natural language:
Words:
an, arrow, banana, flies, fruit, like, time
Syntax: Order of nouns, verbs, predicates, ... Semantics: Time flies like an arrow;
fruit flies like a banana.
The sentence above is an absolutely great example of a completely correct sentence - in English - but with details both someone learning English and a computer would have problems understanding.
- flies - a verb, like an airplaine is flying, or a noun - plural of fly, the insect.
- Time is flying (verb)
- fruit flies (noun)
- like - same spelling and pronunciation for two completely different words, different semantics
- I'm sometimes like, similar to, a young kid.
- And I really like, appreciate, my toys.
- And an English natural language syntax issue - how word can and cannot be put together.
- fruit flies (ok),
- fruit-flies (not), in cases when a '-' is applicable and appropriate, its use changes the meaning of the sentence
- fruitflies (not) - never an accepted way of putting words - in English, as far known to us. But - we're no experts in the English language. Other languages (e.g. Swedish) uses this form all the time to create new words. Ombudsmanskonferensrum - Ombudsman + konferens + rum...
Some additional references:
And like when communicating in a natural language like English, you are also using other disciplines - probably more or less without really thinking too much about them - like mathematics and logic.
As you soon will realize, all the above (words, syntax, semantics, logic, and mathematics) are all crucial components of programming, or using a markup- or query language.
The main difference is we as humans are really smart and easily handles issues with semantics, are forgiving with (minor) grammatical (syntactical) errors, etc, and computers must be told exactly everything - no room for ambiguities.
In English natural language | In JavaScript programming language - correct (intended) semantics |
---|---|
"First I have to pay the bills but if I
Even if a sentence with both serious grammatical and logical problems, we all understand the meaning.
|
$cash_at_hand = check_wallet_subroutine(); $current_bills = check_outstanding_debt_subroutine(); if ($cash_at_hand >= $current_bills) { // Ok, at least have enough dough to pay the bills... $cash_at_hand = $cash_at_hand - pay_bills_subroutine(); if ($cash_at_hand >= 1000) { // great, can also buy a nice present! go_shopping_subroutine( $cash_at_hand ); } } else { call_debtors_to_negotiate_payments(); } |
We'll get back to this example later in regard to intendations, proper writing of code, and debugging (troubleshooting).
< 2. Why Learn Programming? | 4. What Languages to Learn > |