Javascript condition statements and loop statements

Source: Internet
Author: User


Javascript conditional statements include the following:

Single condition structure (if condition statement)
Bidirectional condition structure (if... else condition statement)
Multi-condition structure (switch condition statement)
Single condition structure (if condition statement)


The following types of Javascript loop statements are available:

Test the expression at the beginning of the loop (while loop statement)
Test the expression at the end of the loop (do... while loop statement)
Perform operations on each attribute of an object (for... in loop statement)
Loop controlled by counter (for loop statement)
The syntax of the If condition statement is as follows:

If (expression)
 {
Statement1
 }
The syntax indicates that if the expression condition is met, the statement1 code is executed. Otherwise, the statement1 code is not executed.

The following Javascript example uses the if condition statement of Javascript. First, use. length to calculate the string What's up? And then use the if statement to determine the length of the string. if the length of the string is <100, "the length of the string is less than 100. ".

<Html>
<Head> <title> a Javascript example using the if condition statement </title> <Body>
<Script type = "text/javascript">
Var vText = "What's up? ";
Var vLen = vText. length;
If (vLen & lt; 100)
{
Document. write ("<p> The string length is less than 100. </P> ")
}
</Script>
</Body>
</Html>
Demo

Bidirectional condition structure (if... else condition statement)
The syntax of the If... else condition statement is as follows:

If (expression)
  {
Statement1
  }
Else
  {
Statement2
  }
The syntax indicates that if the expression condition is met, code statement1 is executed. Otherwise, code statement2 is executed.

The following Javascript example uses the if... else condition statement to determine if vHour is less than 17, and "Sun 'an" is displayed; otherwise, "Good Night" is displayed ".

<Html>
<Head> <title> Javascript example using the if... else condition statement </title> <Body>
<Script type = "text/javascript">
Var vDay = new Date ()
Var vHour = vDay. getHours ()
If (vHour <17)
{
Document. write ("<B> cert </B> ")
}
Else
{
Document. write ("<B> Good Night </B> ")
}
</Script>
</Body>
</Html>
Demo

Multi-condition structure (switch condition statement)
The syntax of the Switch condition statement is as follows:

Switch (expression)
 {
Case label1:
Statement1
Break
Case label2:
Statement2
Break
...
Default:
Statementdefault
 }
The syntax indicates that if expression is equal to label1, statement1 code is executed; if expression is equal to label2, statement2 code is executed; and so on. If expression does not conform to any label, execute the statementdefault code in default. The break in the Switch condition statement indicates that the switch statement ends. If one break statement is not used, multiple label blocks are executed.

The following Javascript example uses the switch Condition Statement. Different words are displayed based on the day of the week.

<Html>
<Head> <title> Javascript example using the swith condition statement </title> <Body>
<Script type = "text/javascript">
Var d = new Date ()
TheDay = d. getDay ()
Switch (theDay)
{
Case 5:
Document. write ("<B> It was just Friday. </B> ")
Break
Case 6:
Document. write ("<B> Haha, weekend! </B> ")
Break
Case 0:
Document. write ("<B> I have to go to work again tomorrow. </B> ")
Break
Default:
Document. write ("<B> every workday is as slow as a snail bait! </B> ")
}
</Script>
</Body>
</Html>


Use for loop statements
The for loop statement specifies a counter variable, a test condition, and updates the counter behavior.

You must test the conditions before repeating each loop. If the test succeeds, the code in the loop is executed. If the test fails, the code in the loop is not executed, but the first line of code followed by the loop is executed. When this loop is executed, the counter variable is updated before the next repeating loop.

If the cycle condition persists, the loop is never executed. If the conditions are always met, an infinite loop will occur. The previous one is required in some cases, but the latter is basically not applicable. Therefore, you must pay attention to it when writing loop conditions.

Example code of for loop statement:

<Html>
<Head> <title> a Javascript example using the for loop </title> <Body>
<P>
<Script type = "text/javascript">
For (I = 0; I <= 5; I ++)
{
Document. write (I)
Document. write ("<br> ")
}
</Script>
</P>
</Body>
</Html>
Demo for loop statement example

Use the for... in loop statement
Javascript provides a special way to traverse all user-defined attributes of an object or all elements of an array. In the for... in loop, the cyclic counter is a string, not a number. It contains the name of the current attribute or the subscript of the current array element.

For... in loop statement sample code:

<Html>
<Head> <title> a Javascript example using the for... in loop </title> <Body>
<Script type = "text/javascript">
// Create an object myObject and the three attributes sitename, siteurl, and sitecontent.
Var myObject = new Object ();
MyObject. sitename = "Webmaster Webmasters College ";
MyObject. siteurl = "admin5.com/html ";
MyObject. sitecontent = "Chinese site of the webpage tutorial code library ";
// Traverse all attributes of an object
For (prop in myObject)
{
Document. write ("attribute" + prop + "'is" + myObject [prop]);
Document. write ("<br> ");
}
</Script>
</Body>
</Html>
Demo for... in loop statement example

Use the while and do... while loop statements
The while loop is similar to the for loop. The difference is that the while loop does not have a built-in counter or update expression. If you want to control the loop execution of statements or statement blocks, instead of simply running the code n times, you should use a while loop if you need more complex rules.

Note: the while loop does not have an explicit built-in counter variable, so it is more likely to generate an infinite loop than other types of loops. In addition, since it is difficult to find out when and where the loop condition is updated, it is easy to write a while loop that actually never updates the condition. Therefore, be especially careful when writing a while loop.

Sample Code of the while loop statement:

<Html>
<Head> <title> a Javascript example using the while loop </title> <Body>
<P>
<Script type = "text/javascript">
I = 0
While (I <= 5)
{
Document. write (I + "<br> ")
I ++
}
</Script>
</P>
</Body>
</Html>
Demo while loop statement example

 

In JScript, the do... while loop is similar to the while loop. The difference is that it always runs at least once because it checks the condition at the end of the loop rather than at the beginning.

Do... while loop statement example:

<Html>
<Head> <title> a Javascript example using the do... while loop </title> <Body>
<P>
<Script type = "text/javascript">
I = 0
Do
{
Document. write (I + "<br> ")
I ++
}
While (I <= 5)
</Script>
</Body>
</Html>
Demo do... while loop statement example

Use the break and continue statements
In Javascript, when certain conditions are met, the break statement is used to interrupt a loop. (Please note that you also use the break statement to exit a switch block. See Javascript condition statements ). If it is a for or for... in loop, when updating the counter variable, use the continue statement to skip the remaining code block and directly jump to the next repeating loop.

Break sample code:

<Html>
<Head> <title> a Javascript sample code with a break interrupt loop </title>
<Script type = "text/javascript">
Function BreakTest (breakpoint ){
Var I = 0;
Var m = 0;
While (I <100)
   {
// Interrupt loop when I is equal to breakpoint
If (I = breakpoint)
Break;
M = m + I;
I ++;
   }
Return (m );
}
</Script>
</Head>
<Body>
 
<Script type = "text/javascript">
// Set the value of the BreakTest parameter to 23 to obtain the total value from 1 to 22.
Document. write (BreakTest (23 ))
</Script>
 
</Body>
</Html>
Demo break

 

Sample code of continue:

<Html>
<Head>
<Title> The next repeated Javascript code that uses continue to skip the subsequent code and start the loop </title>
</Head>
<Body>
 
<Script type = "text/javascript">
// The script code is used to output an odd number between 1 and 10.
Var x;
For (x = 1; x <10; x ++)
{
// If x is divisible by 2, skip the code and start the next repetition;
// If x cannot be divisible by 2, execute the following code and output x.
If (x % 2 = 0)
Continue;
Document. write (x + "<br> ");
}
</Script>
 
</Body>
</Html>

 

Related Article

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.