Whitespace and Basic Formatting

What is Whitespace?

Whitespace includes spaces, tabs, and newlines. In C++:

  • Whitespace is mostly ignored by the compiler
  • It's crucial for code readability
  • Good formatting makes code easier to understand and debug

Whitespace Rules

Required Whitespace

Some whitespace is required to separate keywords and identifiers:

// Required spaces
int x;           // Space between 'int' and 'x'
return 0;        // Space between 'return' and '0'

// Would cause errors without spaces
intx;            // Looks like variable name 'intx'
return0;         // Looks like variable name 'return0'

Optional Whitespace

Most whitespace is optional but improves readability:

// Minimal whitespace (legal but hard to read)
int x=5;int y=10;cout<<x+y<<endl;

// Better formatting
int x = 5;
int y = 10;
cout << x + y << endl;

Formatting Guidelines

Spaces Around Operators

// Hard to read
int result=x+y*z-5;

// Much better
int result = x + y * z - 5;

Spaces After Commas

// Cramped
cout<<"Values: "<<x<<","<<y<<","<<z<<endl;

// Readable
cout << "Values: " << x << ", " << y << ", " << z << endl;

One Statement Per Line

// Hard to read
int x=5;int y=10;cout<<x+y;

// Much better
int x = 5;
int y = 10;
cout << x + y;

Indentation

Use consistent indentation to show code structure:

Function Bodies

int main() {
    cout << "Hello World" << endl;
    int x = 5;
    cout << "x = " << x << endl;
    return 0;
}

Nested Blocks

int main() {
    int x = 10;
    if (x > 5) {
        cout << "x is greater than 5" << endl;
        cout << "The value is: " << x << endl;
    }
    return 0;
}

Blank Lines

Use blank lines to separate logical sections:

#include <iostream>

int main() {
    // Get user input
    int age;
    cout << "Enter your age: ";
    cin >> age;

    // Process the input
    bool isAdult = (age >= 18);

    // Display result
    if (isAdult) {
        cout << "You are an adult." << endl;
    } else {
        cout << "You are a minor." << endl;
    }

    return 0;
}

Common Formatting Styles

Style 1: Braces on New Line

int main()
{
    if (condition)
    {
        cout << "Hello" << endl;
    }
}

Style 2: Braces on Same Line (More Common)

int main() {
    if (condition) {
        cout << "Hello" << endl;
    }
}

Tools and Tips

Auto-formatting

Most IDEs can automatically format your code:

  • Visual Studio: Ctrl+K, Ctrl+D
  • Code::Blocks: Format menu
  • Online tools available

Consistency is Key

  • Pick one style and stick with it
  • Follow your team's or project's conventions
  • Good formatting becomes a habit

Why Formatting Matters

Readability

// Unformatted - hard to understand
if(x>0){if(y>0){cout<<"Both positive"<<endl;}else{cout<<"X positive, Y not"<<endl;}}

// Formatted - easy to understand
if (x > 0) {
    if (y > 0) {
        cout << "Both positive" << endl;
    } else {
        cout << "X positive, Y not" << endl;
    }
}

Debugging

Well-formatted code makes it easier to:

  • Spot errors
  • Add debug output
  • Understand program flow
  • Collaborate with others