Quick Index
Index
Condition Examples


Conditions are expressions that result in a true or a false (i.e. a boolean).

These boolean values control program flow (i.e. they are the drivers of the code).

All of these result in true.

We use var name "b" for boolean because it is nice and short.

Note that the var names we use are our choice.

//var to store our boolean
let b;

b = true;
prn("#1: " + b);

b = (1 === 1);
prn("#2: " + b);

b = (10 > 5);
prn("#3: " + b);

b = ('fancy' === 'fancy');
prn("#4: " + b);
All of these result in false.

//var to store our boolean
let b;

b = false;
prn("#1: " + b);

b = (1 === 2);
prn("#2: " + b);

b = (10 > 11);
prn("#3: " + b);

b = ('fancy' === 'hello');
prn("#4: " + b);
The logical NOT operator (!) inverses (flips) a boolean.

It simply flips a true to false, and false to true.

Thus if we take our "truePlay" experiments that all results in true, and apply this operator, all results should be false.

//var to store our boolean
let b;

b = true;
b = !b;
prn("#1a: " + b);

//or we could flip like this
b = !true;
prn("#1b: " + b);

b = 1 === 1;
b = !b;
prn("#2a: " + b);

//or like this
b = !(1 === 1)
prn("#2b: " + b);
The logical AND operator takes two logical expressions (left and right) and is true if both are true.

let b1, b2, b, isSaturday, isSunny, isSunnySaturday;

b1 = true;
b2 = true;
b = (b1 && b2);
prn("expecting true, actual is:  " + b);

b1 = true;
b2 = false;
b = (b1 && b2);
prn("expecting false, actual is:  " + b);

isSunny = true;
isSaturday = true;
isSunnySaturday = isSaturday && isSunny;
prn("Is it a sunny saturday? " + isSunnySaturday);
The logical OR operator takes two logical expressions (left and right) and is true if either is true.

let b1, b2, b, isSaturday, isSunny, isSunnyOrSaturday;

b1 = true;
b2 = false;
b = (b1 || b2);
prn("expecting true, actual is:  " + b);

b1 = false;
b2 = false;
b = (b1 || b2);
prn("expecting false, actual is:  " + b);

isSunny = true;
isSaturday = false;
isSunnyOrSaturday = isSaturday || isSunny;
prn("Is it a sunny or saturday? " + isSunnyOrSaturday);
We can combine logical expressions as needed.

let  isJune, isSunny, isMostlySunny, isNiceJuneDay;

isJune = true;
isSunny = false;
isMostlySunny = true;
/*Let us say that we define a nice day in June as:
	sunny or mostly sunny*/
isNiceJuneDay = isJune && (isSunny || isMostlySunny);
prn("Is it a nice day in June? " + isNiceJuneDay);

More Example Sources


Here are a few (among many more) online related example sources: