Skip to main content

Conditional

// ------------------------------------
// If-Else
// ------------------------------------
if ( /* condition */ )
{
// ...
}
else if ( /* condition */ )
{
// ...
}
else if ( /* condition */ && /* condition */) // "and" operator
{
// ...
}
else if ( /* condition */ || /* condition */) // "or" operator
{
// ...
}
else if ( !/* condition */) // "not" operator
{
// ...
}
else
{
// ...
}
// ------------------------------------
// Ternary
// ------------------------------------

condition ? response_if_true : response_if_false;
// ------------------------------------
// Switch Case Statement
// ------------------------------------

switch ( expression )
{
case value1:
// ...
break; // Optional - If we do not use break, all statements after the matching label are executed.

case value2: // { } are optional but should be used
{
/* A case block is not a new scope by itself, "cases" are labels, just like
"goto" labels.
Any variable you declare within one is visible for the rest of the switch
statement. (But in the other case blocks, it's uninitialized.)
By adding the braces, you create a new scope so the other blocks can't see it.
*/

// ...
break;
}
case value3:
// no break here - It will "Fall-Through" the next case
case value4:
{
// ... - Both "value3" and "value4" will be handled here
break;
}
// ...
default: // Optional. Default code if nothing matches the cases/conditions above
{
// ...
break;
}
}

More Info: