JS, for Loop statement knowledge consolidation, while () {} statement and Do{}while () statement and switch () statement

Source: Internet
Author: User

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+ "&nbsp;&nbsp;")
}
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.