This tutorial focuses on control flow structures and functions. (ie. programming in the small) and includes the following topics:
Conditional Statements
Conditional statements execute a set of other statements only if certain conditions are met. The condition is always enclosed in round brackets. The statements to be performed if the condition is true are enclosed in blocks (ie curly brackets). For example:if (value > 5) { x = 7;}
Occasionally you may want to perform some actions for the false outcome of the condition as well. The else keyword is used to separate branches.if (name == "fred") { x = 4;} else { x = 20;}
NOTE: When the conditional if statement is used only to make an assignment to one variable, you can use the terse C syntax such as:x = (name == "fred") ? 4 : 20;
Loops and Switches
For loops allow a set of statements to be repeated or looped through a fixed number of times. For example to output #1 #2 #3 etc. on separate rows you could write:for (i=1; i<=15; i++) { document.writeln("#"+i); } For-in loops allow looping through all the elements of an array or properties of an object. Unfortunately, it also loops through all object methods and functions as well. The body of every for-in statement should be wrapped in an if statement that does filtering. The filter can select for a particular type or range of values, or it can exclude functions. For example:msg = "navigator object info:\n"; for (propname in navigator) if(typeof navigator[propname]==='string'){ msg += propname + ": " + navigator[propname] + "\n"; } alert(msg); would loop through all the navigator object's properties while disregarding methods and functions. While loops allow a set of statements to be repeated or looped through until a certain condition is met. For example to output #1 #2 #3 etc. on separate rows you could write:i = 1; while (i<=5) { document.writeln("#"+i); i = i + 1; } Switch (or case) statements are used to select which statements are to be executed depending on a variable's value matching a label.yourchoice=window.prompt('pick a number between 1 and 3'); switch (yourchoice) { case '1': alert('you typed a 1'); break; case '2': alert('you typed a 2'); break; case '3': alert('you typed a 3'); break; default: alert('Only 1, 2 or 3 allowed!'); } A problem with this example is that there is only one type of error checking done. What if the user had entered a punctuation sign or typed TWO instead. Also this is a forced example as one could have used the variable yourchoice within the alert method and not used a switch statement at all. Think of the example as just a demonstration of how the switch statement is structured.
Conditional Statements
Conditional statements execute a set of other statements only if certain conditions are met. The condition is always enclosed in round brackets. The statements to be performed if the condition is true are enclosed in blocks (ie curly brackets). For example:if (value > 5) { x = 7;}
Occasionally you may want to perform some actions for the false outcome of the condition as well. The else keyword is used to separate branches.if (name == "fred") { x = 4;} else { x = 20;}
NOTE: When the conditional if statement is used only to make an assignment to one variable, you can use the terse C syntax such as:x = (name == "fred") ? 4 : 20;
Loops and Switches
For loops allow a set of statements to be repeated or looped through a fixed number of times. For example to output #1 #2 #3 etc. on separate rows you could write:for (i=1; i<=15; i++) { document.writeln("#"+i); } For-in loops allow looping through all the elements of an array or properties of an object. Unfortunately, it also loops through all object methods and functions as well. The body of every for-in statement should be wrapped in an if statement that does filtering. The filter can select for a particular type or range of values, or it can exclude functions. For example:msg = "navigator object info:\n"; for (propname in navigator) if(typeof navigator[propname]==='string'){ msg += propname + ": " + navigator[propname] + "\n"; } alert(msg); would loop through all the navigator object's properties while disregarding methods and functions. While loops allow a set of statements to be repeated or looped through until a certain condition is met. For example to output #1 #2 #3 etc. on separate rows you could write:i = 1; while (i<=5) { document.writeln("#"+i); i = i + 1; } Switch (or case) statements are used to select which statements are to be executed depending on a variable's value matching a label.yourchoice=window.prompt('pick a number between 1 and 3'); switch (yourchoice) { case '1': alert('you typed a 1'); break; case '2': alert('you typed a 2'); break; case '3': alert('you typed a 3'); break; default: alert('Only 1, 2 or 3 allowed!'); } A problem with this example is that there is only one type of error checking done. What if the user had entered a punctuation sign or typed TWO instead. Also this is a forced example as one could have used the variable yourchoice within the alert method and not used a switch statement at all. Think of the example as just a demonstration of how the switch statement is structured.