Statements and the Structure of a Program

What is a Statement?

A statement is an instruction that tells the computer to perform some action. In C++, statements are the building blocks of programs.

Types of Statements

Expression Statements

Most common type - expressions followed by a semicolon:

x = 5;        // Assignment statement
cout << x;    // Output statement
++x;          // Increment statement

Declaration Statements

Create and name variables:

int x;        // Declares variable x
int y = 10;   // Declares and initializes y

Compound Statements (Blocks)

Group multiple statements with braces:

{
    int x = 5;
    cout << x;
    x = x + 1;
}

Program Structure

Every C++ program follows this basic structure:

#include <iostream>    // Preprocessor directive

int main() {          // Main function
    // Statements go here
    return 0;         // Return statement
}

Key Points

  • Every statement must end with a semicolon (;)
  • The main() function is where program execution begins
  • Statements execute in order from top to bottom
  • Use proper indentation for readability