Pi Monkeh Dot Net
main
Pi Monkeh!
c++
page 1    page 2     page 3    page 4    page 5    page 6    page 7    page 8

let's learn how to take command in our program, and only do stuff when the fuck we want to do it.

code:
int main()
{
    int x = 1;
    int y = 0;
    if (x > y)
    {
        cout << "x is bigger";
    }
    return 0;
}

this is the if statement. as you might have guessed, it only does things if a condition is met. what if the condition isn't met? well.

code:
int x = 2;
int y = 1;
if (x < y)
    cout << "x is smaller";
else if (x == 3)
    cout << "x is 3";
else
    cout << "x is simply bigger than y, it's not 3 though.";

shit. else if? else? else if is a way of continuing the checking, it's performing another if check if the first if comes up fruitless. else is the path the program takes if nothing above it seems to work out. remember, there can be no else if or else without an if to start it.
did you notice the lack of brackets? in this situation, brackets are optional. if the action under if/else if/else is only one line, a set of brackets is not needed. when you first start out, it's a good idea to use brackets for everything. you suck at programming right now, so use brackets. i'm just showing off. also, note the == sign, this is a comparison operator and not an assignment one. you're checking it against something, not declaring it.

let's look at while and its handicapped friend do.

code:
while (1 > 0)
    cout << "infinite loop\n";

while, like if and friends, doesn't need brackets if the statement underneath is one line. while is pretty straightforward: while (condition) { action; } not much to be said here. i included "\n" which is an easier way to make a new line. the backslash is an escape character, which means something special happens when you put it before certain characters. in n's case, a new line appears. note that \n goes inside quotes, unlike std::endl.

code:
do {
    cout << "infinite loop\n";
} while (1 > 0);

this is the do loop, which is essentially while worded differently. it's ugly as shit and harder to remember (a semicolon comes after the while part). fuck do.

here's the for loop. safety not guaranteed.

code:
for (int i = 0; i < 10; i++)
    cout << i << " ";

holy shit. here we go. for is extremely useful for all kinds of looping, whether it looks it or not. for has one for, two parenthases, and two semicolons. the first part is variable declaration and/or initialization, the second part is a while statement, the last one is something to do while that while statement remains true. let's break it down.

int i = 0;
declaring an integer, i, to be 0. easy enough.

i < 10;
an implied while is there, while i is less than 10...

i++
ah! what the fuck is this? i++ means increment i by 1 (each time i is less than 10 during the check). maybe now you understand the meaning of c++, it's an "improved" c (it's really a "superset" of c, but i digress). remember this: the third part of a for statement never has a semicolon after it.

so. while the new i variable is less than 10, increment it. it's easier (and more stylish) than

code:
int i = 0;
while (i < 10)
    i++;


page 1    page 2     page 3    page 4     page 5    page 6    page 7    page 8