Basic syntax
Definition
Basic syntax is the set of rules that governs how code must be written in a programming language so that it is correctly interpreted or compiled by a computer.
It includes the correct use of:
- Keywords
- Identifiers
- Operators
- Punctuation symbols
- Indentation and formatting
- Statement structure
For example, in many languages a simple assignment statement looks like:
x = 10
This follows syntax rules because x is a variable name, = is the assignment operator, and 10 is a valid value.
Main Content
1. First Concept: Tokens and Keywords
Programming languages are built from small pieces called tokens. Tokens are the smallest meaningful units in code. They include keywords, identifiers, literals, operators, and separators. Among these, keywords are reserved words that have special meanings in the language and cannot be used as ordinary names.
Tokens
Tokens are the basic elements that form a program. Examples include:
ifwhilecount42+;
A compiler or interpreter reads code by breaking it into these units.
Keywords
Keywords are predefined words that tell the computer to perform specific actions. Examples in many languages include:
iffor decision-makingelsefor alternative actionsforfor loopingreturnfor sending back a valueclassfor defining a class
Example:
if age >= 18:
print("Eligible")
Here:
ifis a keywordageis an identifier>=is an operator18is a literal:is a syntax symbol
Why tokens and keywords matter
- They help the language understand the role of each part of the code
- They distinguish between commands and user-defined names
- They prevent ambiguity in program interpretation
2. Second Concept: Identifiers, Variables, and Literals
Identifiers, variables, and literals are central to writing useful code. They allow programmers to store, name, and use data.
Identifiers
An identifier is the name given to program elements such as variables, functions, classes, or objects. A good identifier should be meaningful and follow language rules.
Examples:
totalstudentNamesum_value
Rules for identifiers often include:
- Must begin with a letter or underscore
- Cannot start with a number
- Cannot use keywords as names
- Should not contain spaces
Example of valid and invalid identifiers:
name = "Asha" # valid
student1 = 25 # valid
2value = 10 # invalid
class = "Math" # invalid because class is a keyword
Variables
A variable is a named storage location that holds data. Variables make programs flexible because values can change while the program runs.
Example:
marks = 85
marks = 90
Here, marks stores a value, and the value can be updated.
Literals
A literal is a fixed value written directly in code. Examples:
- Integer literal:
10 - Floating-point literal:
3.14 - String literal:
"Hello" - Boolean literal:
True
Example:
age = 20
name = "Ravi"
is_passed = True
Relationship among them
ageandnameare identifiers20and"Ravi"are literalsage = 20is a variable assignment statement
Why they matter
- Identifiers make code readable
- Variables let programs store changing data
- Literals provide actual values for computation
3. Third Concept: Operators, Statements, and Formatting Rules
This concept explains how code performs actions and how its structure must be arranged for correct execution.
Operators
Operators are symbols that perform operations on values or variables.
Common types:
- Arithmetic operators:
+,-,*,/,% - Relational operators:
==,!=,>,<,>=,<= - Logical operators:
and,or,not - Assignment operator:
=
Examples:
sum = 5 + 3
is_valid = age >= 18
result = score > 50 and attendance > 75
Statements
A statement is a complete instruction that the computer can execute. A program is made up of one or more statements.
Examples:
x = 10
print(x)
if x > 5:
print("Large")
Each statement has a specific purpose:
- assignment
- output
- decision-making
- repetition
Formatting rules
Formatting rules make code easier to read and often determine whether the code runs correctly.
Important formatting aspects include:
- Proper indentation
- Line breaks
- Use of punctuation such as colons, semicolons, or braces depending on the language
- Consistent spacing
Example of readable code:
if score > 50:
print("Pass")
else:
print("Fail")
In languages like Python, indentation is part of syntax. In other languages, braces may be used:
if (score > 50) {
printf("Pass");
} else {
printf("Fail");
}
Why these rules matter
- Operators define the action being performed
- Statements express complete instructions
- Formatting ensures clarity and, in some languages, correctness
Simple structure view
[Identifier] [Operator] [Value]
x = 10
This shows the common structure of an assignment statement.
Working / Process
-
Identify the language rules
First, learn the syntax rules of the programming language being used. Every language has its own keywords, symbols, indentation style, and statement format. For example, Python uses indentation, while C uses braces and semicolons. -
Break the code into components
Read the code by separating it into tokens such as keywords, identifiers, operators, literals, and punctuation marks. This helps determine whether each part is used correctly. -
Write and check statements carefully
Construct each statement according to the language rules, then verify punctuation, spelling, order, and formatting. Even a small mistake like a missing colon, bracket, or quotation mark can produce a syntax error.
Advantages / Applications
- Helps write correct programs that the computer can understand and execute
- Improves code readability, making it easier for humans to review, debug, and maintain
- Forms the foundation for learning advanced programming concepts such as functions, loops, classes, and data structures
Summary
- Basic syntax is the rule system used to write valid code
- It includes tokens, identifiers, variables, operators, and statements
- Correct syntax is necessary for a program to run properly
- Important terms to remember: keyword, identifier, variable, literal, operator, statement, indentation