The for Loop

Download Report

Transcript The for Loop

Tonight’s JavaScript Topics
• Conditional Statements: if and switch
• The Array Object
• Looping Statements: for, while and do-while
Conditional Statements in JavaScript
• A conditional statement is a statement that
runs a command or command block only
when certain conditions are met.
The if Statement in JavaScript
• To test a single condition, use:
if (condition) {
commands
}
• To test between two conditions, use:
if (condition) {
commands if condition is true
} else {
commands if otherwise
}
The if Statement in JavaScript (cont.)
• To test multiple conditions, use:
if (condition 1) {
first command block
} else if (condition 2) {
second command block
} else if (condition 3) {
third command block
} else {
default command block
}
The Switch Statement in JavaScript
• To test for different values of an expression, use:
switch (expression) {
case label1: commands1
break;
case label2: commands2
break;
case label3: commands3
break;
...
default: default commands
}
• The break statement terminates any program loop or conditional.
A Complete Example
• Let’s modify last week’s dice program to play
a simplified version of craps:
– if the dice roll is 2, 3 or 12, the user loses (craps)
– if the dice roll is 7 or 11, the user wins
– any other number and the user continues rolling
• We’ll use if statements to do the job. As an
outside-class exercise, try changing the
program to use a switch statement.
The Array Object in JavaScript
• An array is a collection of data values organized under a
single name
– Each individual data value is identified by an index
• To create an array:
var array = new Array(length);
• To populate an element of the array:
array[i] = value;
• To create and populate an array:
var array = new Array(values);
• To get the size of the array, use the property
array.length
Some Useful Array Methods
A Program Example Using an Array
• Let’s write a JavaScript program that does the
following:
– displays a button on the web page
– whenever the user presses the button, a random
fortune will be displayed to the user.
– the list of “random” fortunes will be stored in an
array
Looping in JavaScript: The for Loop
• A program loop is a set of commands executed
repeatedly until a stopping condition has been
met
• for loops use a counter variable to track the
number of times a block of commands is run
• To create a for loop, use:
for (start at; continue until; update by){
commands to repeat
}
A for Loop Example
Another for Loop Example
• for loops are often used to cycle through the
different values contained within an array:
Loops in JavaScript: The while Loop
• A while loop runs as long as a specific condition
is true
• To create a while loop, use:
while(continue until condition){
commands to repeat
}
• Example: var sum = 0;
while(sum < 10) {
sum = sum + Math.random();
}
Loops in JavaScript: The do-while Loop
• A do-while loop runs as long as a specific condition
is true but the condition is at the end
• To create a do-while loop, use:
do {
commands to repeat
while(continue until condition);
• A do-while loop is always executed at least one
time which isn’t necessarily true of while loops.
• Watch out for infinite loops!
A Final Example: Rock, Paper, Scissors
1. For player 1, generate a random # between 1 and 3.
1= rock, 2=paper, 3-scissors.
2. For player 2, generate a random # between 1 and 3.
1= rock, 2=paper, 3-scissors.
3. Figure out which player wins by the rules:
- paper covers rock (paper wins)
- scissors cuts paper (scissors win)
- rock breaks scissors (rock wins)
- both players have the same item (tie)
4. Display an alert message like:
“Player 1 has rock. Player 2 has paper. Player 2 wins.”
5. Keep playing if they tie until one player wins.