JavaScript Reference: Statements and Control Flow
If Statements
If statements are used to execute a block of code, given particular conditions are true.
if
if...else
if...else Shorthand Notation
if...else if
For Loops
For loops are used to execute a block of code a particular number of times, or for each element in an array or object.
for
- Use: Execute a block of code a fixed number of times.
- Format: for ( initial value; condition; value increment ) { statement }
- Example:
for ( var i=0; i < 5; i++ ) {
console.log( "This message will print until i is 5, but right now it's only " + i)
}
would result in the following output:
This message will print until i is 5, but right now it's only 0
This message will print until i is 5, but right now it's only 1
This message will print until i is 5, but right now it's only 2
This message will print until i is 5, but right now it's only 3
This message will print until i is 5, but right now it's only 4
for...in
- Use: Execute a block of code for each element in an array, or the names of properties of an object.
- Format: for ( element in array ) { statement }
- Example: A for loop over array elements.
var breeds = ["Lhasa Apso","Shih Tzu","Shiba Inu"]
for ( b in breeds ) {
console.log( b )
}
would result in the following output:
Lhasa Apso
Shih Tzu
Shiba Inu
- Example: A for loop over object elements.
var dog1 = { breed:"Dalmation", name:"Spot", color:"Black and white" }
for ( x in dog1 ) {
console.log( dog1[x] + ' is my ' + x )
}
would result in the following output:
Dalmation is my breed
Spot is my name
Black and white is my color
While Loops
While loops are used to execute a block of code until a condition changes.
while
- Use: Execute a statement as long as a condition is true. Note that the condition is tested before the block is executed.
- Format: while ( condition ) { statement }
- Example:
var x = 0
while (x <= 5) {
console.log( "The remainder of " + x + " when divided by 3 is " + x%3)
x += 1
}
would result in the following output:
The remainder of 0 when divided by 3 is 0
The remainder of 1 when divided by 3 is 1
The remainder of 2 when divided by 3 is 2
The remainder of 3 when divided by 3 is 0
The remainder of 4 when divided by 3 is 1
The remainder of 5 when divided by 3 is 2
- Example:
var x = 1
while (x < 1) {
console.log( x )
x += 1
}
would result in no output.
do...while
- Use: Execute a statement as long as a condition is true. Note that the condition is tested after the block is executed.
- Format: do { statement } while ( condition )
- Example:
var x = 0
do {
console.log( "The remainder of " + x + " when divided by 3 is " + x%3)
x += 1
}
while (x <= 5)
would result in the following output:
The remainder of 0 when divided by 3 is 0
The remainder of 1 when divided by 3 is 1
The remainder of 2 when divided by 3 is 2
The remainder of 3 when divided by 3 is 0
The remainder of 4 when divided by 3 is 1
The remainder of 5 when divided by 3 is 2
- Example:
var x = 1
do {
console.log( x )
x += 1
}
while (x < 1)
would result in the following output:
1
Switch Statements
- Use: Execute a block of code based on the value (case) of an expression. Frequently paired with
break
so the switch stops execution once a match is found. An optional default statement will run if no match is found.
- Format: switch ( expression ) { case case_1: statement_1; case case_2: statement_2 }
- Example:
var breed = prompt("What kind of dog do you have?")
switch ( breed ) {
case "English Bulldog":
alert("Bulldogs are courageous but gentle and were bred to bait bulls.")
break;
case "Clumber Spaniel":
alert("Clumber spaniels are calm but affectionate and originated in France.")
break;
case "Labrador Retriever":
alert("Labs are athletic and are excellent swimmers.")
break;
case "Papillon":
alert("Papillons are small but intelligent dogs with long hair and big ears.")
break;
default:
alert("Sorry, I don't know about your dog!")
}
Try it
Break and Continue
break
- Use: Exit a loop if a condition is true
- Example:
var t = [1,2,3,4,5,6,7,8,9,10]
// the loop below searches through the array, but ends the search when 5 is found
for (i in t) {
if (i == 5) {
console.log("Found it");
break;
}
else {
console.log("Haven't found 5 yet")
}
}
continue
- Use: Skip an iteration of a loop (and continue to next) if a condition is true
- Example:
var t = [1,2,3,4,5,6,7,8,9,10]
// the loop below prints all entries of the array except 4:
for (i in t) {
if (i == 4) {
continue
}
else {
console.log( i )
}
}