Notes from 10/03

for-loop.cpp

#include <iostream>
using namespace std;
int main()
{
    // count to 10
    int i = 1;     // Initialize
    while(i <= 10) // Condition
    {
        cout << i; // Body
        i++;       // Update
    }
    cout << "\n";
    // "for" loop syntax makes this more clear.
    int j;
    for(j = 1 ; j <= 10 ; j++ )
    {   // Body
        cout << j;
    }

    return 0;
}

increment.cpp

#include <iostream>
using namespace std;
int main()
{
    int x = 5;
    float y = 3.0;

    x + 1;      // Add one but doesn't change x
    x = x + 1;  // Add one to x, and store back into x
    x += 1;     // Shortcut for previous

    y *= 1.5;  // expands to
    y = y * 1.5;
    cout << y << "\n";    

    int z = 2;
    z *= z + 1; // expands to z = z * (z+1) // note parens
    cout << z << "\n";
    
    // especially common to add or subtract 1 from an int
    x = x + 1;
    x += 1;
    x++;        // Add one, changes x, same as above
                // ++ is called "increment" operator

    z--;        // Subtract one, changes z
    z -= 1;     // These are all the same
    z = z - 1;  // -- is called "decrement" operator

    ++x; // Same as post-increment except for one detail
    --z; // Same as post-decrement, except for one detail

    x++; // adds one to x, changes x, but RETURNS old value
    ++x; // adds one to x, changes x, but RETURNS new value

    cout << "A using postfix\n";
    int a = 3;
    cout << a << "\n";  // 3
    cout << a++ << "\n"; // 3 (old value)
    cout << a << "\n"; // 4

    cout << "B using prefix\n";
    int b = 3;
    cout << b << "\n";  // 3
    cout << ++b << "\n"; // 4 (new value) 
    cout << b << "\n"; // 4

    int d = 8;
    int e = 4;

    cout << ++d * e++ << "\n";
    // 9 * 4 == 36




    return 0;
}

stars2.cpp

#include <iostream>
using namespace std;
int main()
{
    int i, j, n;
    cout << "Enter size: ";
    cin >> n;
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            // hollow box
            //if(j == n || j == 1 || i == 1 || i == n)
            // horizontal and vertical lines
            // if(j%2 == 1 || i%2 == 1)
            // checkerboard
            if((i+j)%2 == 0)
            {
                cout << "* ";
            }
            else
            {
                cout << "  ";
            }
        }
        cout << "\n";
    }
    return 0;
}

stars.cpp

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "Enter size: ";
    cin >> n;
    // print n stars across
    int i = 1;     // counting loop
    while(i <= n)
    {
        cout << "* ";
        i++;
    }
    cout << "\n";

    // Middle   
    for(i = 1; i <= n-2; i++)
    {
        cout << "* ";
        int j;
        for(j = 1; j <= n-2; j++)
        {
            cout << "  ";
        }
        cout << "*\n";
    }
    // bottom row: n stars across
    for(i = 1; i <= n; i++)
    {
        cout << "* ";        
    }
    cout << "\n";

    return 0;
}