One, for Loop statement practice
About the two problem types that exist for a for loop
Poor lift: Do not know what the situation is really to find our results, we can let one after another go through.
Iterative: In the existing conditions according to the law of constant solution, the intermediate situation, the final inferred results
1, 99 multiplication table
<script>
for (Var i=1;i<=9;i++)
{
for (j=1;j<=i;j++)
{
document.write (j+ "*" +i+ "=" +i*j+ " ")
}
document.write ("<br/>")
}
</script>
2, 100 within the base of the combined and (iterative cycle of death)
<script>
var sum=0
for (Var i=1;i>0;i+=2)
{
Sum+=i
if (i==99)
{
alert (sum);
Break
}
}
</script>
3, the creation of man-machine stone scissors cloth
<body>
<input type= "text" id= "Shuzi"/>
<input type= "button" value= "click" onclick= "ABC ()"/>
</body>
<script>
function ABC ()
{
var A=document.getelementbyid (" Shuzi "). Value;
var b=parseint (Math.random ())
Var c
if (a!= "")
{
F (a== "stone")
{
C=0
}
else if (a== "scissors")
{
C=1
}
Else if (a== "cloth")
{
c=2
}
Else
{
Alert ("Input error")
}
Var s=c-b
if (b==0)
{
var d= "stone"
}
Else if (b==1) br> {
var d= "Scissors"
}
Else if (b==2)
{
var d= "cloth"
}
Switch (s)
{
Case 0:alert ("Computer out" +d+ "draw"), break
Case-1:alert ("Computer out" +d+ "man wins"), break
Case 2:alert ("Computer out" +d+ "man wins");
Case 1:alert ("Computer Out" +d+ "machine wins"); break
Case-2:alert ("Computer Out" +d+ "machine wins"); Break
}
}
}
</scrip T>
Second, while Loop statement
It is used in the same way as a For loop, and its structure is different
I=1
while (I<=a)
{
Loop body
I=i+1
}
Three, do and loop statements
It differs from the while statement in that the loop body is cycled first, then the loop condition is determined, the true loop, false output
I=i+1
Do
{
Loop body
i++
}
while (I<=a)
Iv. switch statement: Make a multiple-choice statement
Switch (variable name)
{
Case 1:alert ();
Case 2:alert ();
Case 3:alert ();
Case 4:alert ();
}
Five, two keywords
1, break: Meet the conditions to jump out of the entire cycle
2, continue: Skip this condition, then continue the next cycle process
JS, for Loop statement knowledge consolidation, while () {} statement and Do{}while () statement and switch () statement