The Sysadmin Notebook  

Sitemap

Conditional Statements

Syntax for simple conditional statements

if...else

if (condition) {
	//do something
}
else {
	//do something else
}

Switch statements

switch(condition) {
	case conditionOne:
	//code to execute 
	break; 
	case conditionTwo:
	//code to execute
	break; 
	case conditionThree:
	//code to execute
	break;
	default:
	//code to execute if no conditions match
}

'break' can also be used to prematurely terminate nested ifs

The for loop

for (initial-condition; loop-test; increment) {
	//code to execute
}
for (index in array) {
	//code to execute
}

The while loop

while (condition) {//code to execute}

'continue' can be used to terminate the remainder of a block and continue the loop at the next iteration

The do..while loop

The do..while guarantees the code will execute at least once

do {//code to execute} while (condition)

The ternary operator

var variable = condition ? trueValue:falseValue;