Conditional Statements

xkcd conditionals

Logic? Logic!

Geometry is logic and logic is the battlefield of adulthood. - Unknown

Example:

// shoe choices in my program
if(it’s raining outside) {
    wearBoots();
}
else {
    // not raining
    if(it’s above 65 degrees) {
        wearSandals();
    }
    else {
        wearSneakers();
    }
}

boolean

booleans -> true or false values

In code: boolean

int x = 10;
boolean imCool = false;

if

if statement:

In ~English:

if A is true -> then do XYZ

if A is false -> then do 123 instead

In (pseudo)code:

if(A == true) {
    do XYZ
}

if(A == false) {
    do 123
}

else & else if

else:

else if:

In ~English:

if B is true -> then do the dishes

else if C is true -> play Halo (infinity or whatever damn version)

else -> do meh homework

In (pseudo)code:

if(B == true) {
    doTheDishes();
}
else if(C == true) {
    playHalo();
}
else {
    doMehHomework();
}

AND

AND -> both things are true

In ~English:

if A and B are true -> then do something

|
V

if A is true -> then if B is true -> then do something

In (pseudo)code:

if(A is true AND B is true) {
    doSomething();
}

OR

OR -> either of the things are true

In ~English:

if A or B are true -> then do something

In (pseudo)code:

if(A is true OR B is true) {
    doSomething();
}

Using booleans Directly

In ~English:

I’m cool = true

if I’m cool -> I get the night action

if I’m not cool -> I eat ice cream and watch Netflix (tomorrow is a better day)

In (pseudo)code:

// declare boolean variable
boolean imCool = true;

if(imCool == true) {
    nightAction();
}

// imCool == false
if(imCool != true) {
    iceCreamNetflix();
}

|
V

// short for imCool == true
if(imCool) {
    nightAction;
}

NOT

! -> not or negation, inverts the boolean value

Short for imCool == false or imCool != true

Reads like “if I’m NOT cool”

In (pseudo)code:

if(!imCool) {
    iceCreamNetflix();
}