In this program we are taking it as data_input the number of lines that you want to print. We then set ct to 1. The ct will be used to count the lines we write. The while loop is done while ct is <= data_input. Inside the loop you write a line which includes the ct. You then add 1 to ct. When the add 1 to ct makes ct no longer <= data_input. At that point, control drops out of the while loop and does the document write which says This is the end!!!"> In this program we are taking it as data_input the number of lines that you want to print. We then set ct to 1. The ct will be used to count the lines we write. The while loop is done while ct is <= data_input. Inside the loop you write a line which includes the ct. You then add 1 to ct. When the add 1 to ct makes ct no longer <= data_input. At that point, control drops out of the while loop and does the document write which says This is the end!!!">
Download presentation
Presentation is loading. Please wait.
Published byAngel McCoy Modified over 9 years ago
1
JavaScript Loops Please use speaker notes for additional information!
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> This is the while loop... <!-- var data_input=prompt("How many times should I write the line?",0); ct=1; while (ct <= data_input) { document.write("This is number " + ct + " "); ct = ct + 1; } document.write(" " + "This is the end!!!"); //--> In this program we are taking it as data_input the number of lines that you want to print. We then set ct to 1. The ct will be used to count the lines we write. The while loop is done while ct is <= data_input. Inside the loop you write a line which includes the ct. You then add 1 to ct. When the add 1 to ct makes ct no longer <= data_input. At that point, control drops out of the while loop and does the document write which says This is the end!!!
4
ct=1; while (ct <= data_input) { document.write("This is number " + ct + " "); ct = ct + 1; } ct=1 ct<= data_input Document. write including ct ct=ct + 1 Y N This flowchart shows the while loop with the decision being made prior to entering the loop.
7
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> This is the while loop... <!-- //The do...while executes the statements in the loop ones and then repeats the //the execution while the condition is true. This means the do...while executes //at least once. This is the difference between the do...while and the while that //we looked at earlier. The while evaulates the condition before executing so the //statements in the loop may never be executed. var data_input=prompt("How many times should I write the line?",0); ct=1; do { document.write("This is number " + ct + " "); ct = ct + 1; } while (ct <= data_input) document.write(" " + "This is the end!!!"); //-->
8
ct=1 ct<= data_input Document. write including ct ct=ct + 1 Y N ct=1; do { document.write("This is number " + ct + " "); ct = ct + 1; } while (ct <= data_input) This shows the loop where the loop is entered and the code execute and then the condition is checked to determine whether or not the loop should be executed again.
11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + ans + " "); } //--> i j 1 1 2 3 4 2 1 2 3 4 3 1 2 3 4 The for loop initializes, states the condition to stay in the loop and gives the increment value.
13
As shown on the last slide, 5 was entered and this triggered the wrong answer response. Note that I have now had j run through 1, 2, 3 and now I am back to the outer loop and i has been incremented by 1and is now 2 and j has been reset to 1 because the j for loop was reached through dropdown.
14
On the last pass, both i and j are 3. Note that each time the I gets incremented, the j gets reset to 1. i j i j 1 1 2 3 4 2 1 2 3 4 3 1 2 3 4
15
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } //--> i j 1 1 2 3 4 2 1 2 3 4 3 1 2 3 4
17
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- // If the user presses cancel the input is null - this program tests for null // Break exits out of the closest loop - note that I then set i to 4 to end the outer // loop - some programmers would not choose this approach because they avoid this // kind of manipulation
18
for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (user_input != null) { if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } else { i=4; break; } //-->
19
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Math Facts div { text-align: center; } MATH FACTS 1..3 <!-- // If the user presses cancel the input is null - this program tests for null // Break exits out of the closest loop - note that I then set endFlag to Y and // when I return to the outer loop I test to see if the endFlag is Y which // means I want to break out of that loop as well // Note also that null and empty are not the same thing = "" is empty
20
var endFlag = "N" for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (user_input != null) { if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } else { endFlag="Y"; break; } if (endFlag == "Y") { break; } //-->
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.