IT 102: Programming Language Structure, Syntax, Semantics, Keywords, and Identifiers

Course Details

  • Course Code & Name: IT 102 – Computer Programming 1
  • Topic: Programming Language Structure, Syntax and Semantics, Keywords, and Identifiers
  • Instructor: Sitti Mariam M. Jolo

Programming Languages Overview

  • Definition: A programming language is a formal system used to write instructions that computers can process.
  • Structural Rules: Similar to human languages, programming languages possess specific rules governing code construction. In C++, rules strictly govern:
    • Writing statements
    • Naming variables
    • Using symbols
    • Declaring variables
    • Defining functions
    • Organizing program instructions
  • These governing rules form the syntax of C++.

Syntax and Syntax Errors

  • Syntax Definition: Syntax refers to the precise rules that determine how symbols, words, and statements must be arranged in a programming language.
    • Valid C++ Statement: cout << "Hello World";
    • Invalid C++ Statement: cout << "Hello World" (Fails because it omits the required statement terminator ;).
  • Syntax Error Definition: A syntax error occurs when a programmer violates the grammatical rules of the programming language.
  • Common Categories of Syntax Errors:
    • Missing Semicolon:
    • Incorrect: cout << "Hello"
    • Correct: cout << "Hello";
    • Missing Quotation Mark:
    • Incorrect: cout << "Hello;
    • Correct: cout << "Hello";
    • Missing Closing Brace:
    • Incorrect: int main() { cout << "Hello";
    • Correct: int main() { cout << "Hello"; }
    • Incorrect Capitalization:
    • C++ is strictly case-sensitive.
    • cout is distinct from Cout.
    • The correct standard output command is lower-case cout.

Syntax Error Exercises

  • Exercise 1: Code cout<<“Good Morning”
    • Identified Error: Missing semicolon ; at the end of the statement.
  • Exercise 2: Code Cout<<“Welcome”;
    • Identified Error: Incorrect capitalization of Cout (must be lowercase cout).
  • Exercise 3: Code cout << Welcome";
    • Identified Error: Missing opening double quotation mark " before Welcome.
  • Exercise 4: Code int main(
    • Identified Error: Missing closing parenthesis ), opening brace {, and closing brace }.
  • Exercise 5: Code return 0
    • Identified Error: Missing semicolon ; at the end of the return statement.

Semantics

  • Semantics Definition: Semantics refers to the meaning or purpose of a programming statement (what the code actually performs).
  • Semantic Evaluation Examples:
    • Statement: int age = 20;
    • Syntax: Correct C++ declaration and initialization syntax.
    • Semantics: Directs the computer to create an integer variable named age and assign it the numeric value 20.
    • Statement: cout << "Welcome";
    • Semantics: Directs the computer to display the word Welcome on the screen.

Syntax vs. Semantics Comparison

Syntax vs Semantics Concept Table

  • Comparative Summary Table:
    • Syntax:
    • Meaning: Rules for writing code.
    • Core Question: "Is the code written correctly?"
    • Semantics:
    • Meaning: Meaning of the code.
    • Core Question: "What does the code mean or do?"
  • Illustrated Example (int age = 18;):
    • Syntax: The statement follows the correct structural syntax of C++.
    • Semantics: It creates an integer variable named age containing the value 18.
  • Diagnostic Exercises (Classifying Syntax vs. Semantics):
    1. Scenario: A semicolon is missing after a statement. -> Syntax
    2. Scenario: The formula adds two numbers when it should multiply them. -> Semantics
    3. Scenario: The opening brace { has no matching closing brace }. -> Syntax
    4. Scenario: The program calculates an incorrect average. -> Semantics
    5. Scenario: A quotation mark is missing. -> Syntax

Basic Structure of a C++ Program

C++ Code Breakdown

  • Standard Program Template: ```cpp

    include

    using namespace std;

    int main() { cout << "Hello, World!" << endl; return 0; }   ```

  • Detailed Explanation of Core Components:

    • #include: Preprocessor directive that instructs the preprocessor to include the contents of a header file or library prior to code compilation.
    • <iostream>: Standard Input/Output Stream header file. Provides necessary I/O facilities:
    • cout: Standard character output stream.
    • cin: Standard character input stream.
    • endl: Line ending manipulator used to insert a newline character and flush the stream.
    • using namespace std;: Using-directive. Places standard library names into the global namespace, allowing convenient access without prefixing std::.
    • int main(): Main function identifier. Serves as the mandatory execution entry point for standard C++ console applications.
    • { }: Curly braces defining a code block, indicating the start and end of function definitions or statement blocks.
    • cout: Standard output stream object used to send text and data to the console display.
    • <<: Stream insertion operator; directs the right-hand operand into the output stream.
    • "Hello, World!": Text contained within double quotation marks, categorized as a string literal.
    • ;: Semicolon serving as the required statement terminator.
    • return 0: Return statement that terminates the main() function and yields the integer status code 0 to the operating system (0 signals success).

Program Structure Analysis

Structure Analysis Table

  • Component Classification Table:
    • #include <iostream> — Preprocessor directive
    • iostream — Standard input/output header
    • using namespace std; — Allows convenient access to names in std
    • int — Keyword / data type
    • main — Main function identifier
    • { } — Defines a block
    • cout — Standard output stream
    • << — Stream insertion operator
    • "Hello, World!" — String literal
    • ; — Statement terminator
    • return — Keyword
    • 0 — Integer literal

Comments in C++

Multi-line Comment Example

  • Definition: Comments are explanatory notes written within the source code strictly for developers.
  • Compiler Treatment: Comments are completely ignored by the C++ compiler during execution.
  • Comment Styles:
    • Single-line Comments: Denoted by // (e.g., // Hello).
    • Multi-line Comments: Enclosed between /* and */ (e.g., /* This is a Multiple line Comment */).

Keywords

  • Definition: Reserved words that possess explicit, predefined meanings within the C++ programming language.
  • Usage Constraint: C++ keywords cannot be used as custom programmer-defined identifiers.
  • List of C++ Keywords:
    • int
    • return
    • if
    • else
    • while
    • for
    • double
    • float
    • char
    • bool
    • void
    • switch
    • Case
    • break
    • continue
    • class
    • public
    • private

Identifiers and Rules

  • Definition: Custom identifiers created by the programmer to name program elements.
  • Identifier Uses: Variables, functions, classes, arrays, objects, and other user-defined entities.
  • Examples: age, studentName, totalScore, averageGrade, calculateTotal.
  • Six Standard Rules for C++ Identifiers:
    • Rule 1: May Contain Letters
    • Valid: student, total, average.
    • Rule 2: May Contain Numbers, but Cannot Begin with a Number
    • Valid: student1, score2, grade2026.
    • Invalid: 1student, 2score, 2026grade.
    • Rule 3: May Contain an Underscore (_)
    • Valid: student_name, total_score, first_name.
    • Rule 4: Spaces Are Prohibited
    • Invalid: student name, total score.
    • Rule 5: Keywords Cannot Be Used as Identifiers
    • Invalid: int return;, int for;, int class; (since return, for, and class are reserved keywords).
    • Rule 6: C++ Identifiers Are Case-Sensitive
    • score, Score, and SCORE represent three completely distinct identifiers.
    • Mixing cases arbitrarily creates confusion and logical/syntax errors.

Literals

  • Definition: A literal is a fixed value written directly into the source code.
  • Categories and Examples:
    • Integer Literal: 25
    • Decimal Literal: 95.5
    • Character Literal: 'A'
    • String Literal: "Computer Programming"
    • Boolean Literal: true, false
  • Literal Analysis Example (int age = 20;):
    • int: Keyword (data type specifier)
    • age: Identifier (variable name)
    • =: Assignment operator
    • 20: Integer literal
    • ;: Statement terminator

Statements

C++ Statements Classification

  • Definition: A statement is a single complete instruction executed in a C++ program.
  • Examples: int age = 20;, cout << "Hello";, return 0;.
  • Statement Classification Table:
    • #include <iostream> — Preprocessor directive
    • using namespace std; — Using-directive
    • int age = 20; — Declaration/initialization statement
    • cout << "Hello"; — Output statement / expression statement
    • return 0; — Return statement
    • // Hello — Comment

References

  • Course Syllabus and CCS Student Handbook
  • CMO No. 25, series of 2015
  • Zak, D. (2009). An Introduction to Programming with C++
  • Ahmad, S. S. (2024). Fundamentals of Programming
  • Downey, A. B. (n.d.). Think C++: How to think like a computer scientist. Green Tea Press
  • James, Jason (2026). Exploring C++: The Adventure Begins
  • Soulie, Juan (2007). C++ Language Tutorial