Programming in C++

Table of Contents

Basic functionality

Variables

int inch;
bool answer=true;

Fundamental types

from these, you can construct:

Operators

Tests (conditionals)

// simple function that asks user to proceed
bool accept() {
    cout << “Do you want to proceed (yN)?\n”;
    char answer=0;
    cin >> answer;

    if (answer == ‘y’) return true;
    return false;
}

// more complex function, with a switch statement
bool accept2() {
    cout << “Do you want to proceed (yN)?\n”;
    char answer=0;
    cin >> answer;
    switch (answer) {
    case ‘y’:
        return true;
    case ’n’:
        return false;
    default:
        cout << “Whatever, it’s a no.\n”;
        return false;
    }
}

Loops

while (condition) {
    // execute code
    // opt. increment iterator
}

for (int i=0; i<10; i++) {
    // execute code
}

Pointers & arrays