js02-Common Flow control statements

Source: Internet
Author: User

1.if Statements

Syntax: if (condition) {

Execution when conditions are in existence  

}else{

Conditions do not set up the implementation

}

Cases

var ji  =;         if (ji>=20) {            Console.log (' congratulate you, eat chicken success, good fortune ')        }else{            Console.log ( ' I'm sorry to keep working hard next time ')                    }

2.switch Statements

Syntax: switch (value) {

Case value 1:

EXECUTE statement

Break

Case Value:

EXECUTE statement

Break

}

<script type= "Text/javascript" >//switch ("better") represents the condition. Case represents a statement that executes after a condition is met        Switch("Better0") {             Case"Good": Console.log ("Great place to stay")                 Break;  Case"Better": Console.log ("Very good")                 Break;  Case"Best": Console.log ("Eat ducks.")                 Break; default: Console.log ("The real dishes to play")                 Break; }            </script>
3while and do While statements

While syntax:

while (condition) {
Execute code block
}

<script type= "Text/javascript" >//Whilex Cycle                //1. Initialize the loop variable 2. Determine the loop condition 3, update the loop variable        vari = 1;//1. Initialize the loop variable                 while(i<=9) {//2. Judging the cycle conditions//Console.log (i)I= i+1;//3, update the loop variable. Remember these three steps        }                //Practice                //output all multiples of 1-100 between 3        varj = 1;  while(j<=100){            if(j%3===0) {Console.log (j)} J++; }            </script>
Do While syntax

Note: The do side executes the code block after do once and then judges the while condition.

do{
Execute code block
} while (condition);

<script type= "Text/javascript" >        //WhileLoop trilogy        //1. Initialize the variable   2. Judging the cycle Condition   3. Update the loop variable        // whether the while condition is true or not, the code in do is run first, and then the while is judged.        var i = 5;          Do {            Console.log (i)            i++        ;  while (i<10);     </script>
4 for statement

Grammar:

for (Var 1=10 initializes the variable, i>=1 is the condition, i--is the update variable. Use between each statement; separate)
for (var i = ten; i>=1; i--) {
Console.log (i)
}

<script type= "Text/javascript" >//For (var 1=10 initializes the variable, i>=1 is the condition, i--is the update variable. Use between each statement; separate)         for(vari = 10; i>=1; i--) {Console.log (i)}//even number of outputs 1-100         for(vari = 1; I <= 100; i++) {            if(i%2==0) {Console.log (i)}}//calculate between 1-100 and        //var sum=0        //For (var i = 1; i <=; i++) {        //sum+=i        //}console.log (sum)        //For (var i = 1; I <=3; i++) {        //For (var x =1; <=6; x + +) {        //document.write ("*")                        //}document.write ("<br>")                    // }</script>
array:
<script type= "Text/javascript" >/*How data is created: 1. Literal way 2. Function Construction Method*/        //The literal way, recommended this way. Because it's simple and straightforward.        varColors =["Red", "green", "blue"]; Console.log (colors)//using the constructor new        varColors2 =NewArray ("Balck", "white", "origin"); Console.log (COLORS2)//Array Assignment        varARR1 = []; arr1[0] = "www.google.com"; arr1[1] = "wwww.it.com"; arr1[2] = "www.sohu.com"; arr1[3] = "www.sina.com"; arr1[4] = "www.163.com"; //Console.log (arr1)         for(vari = 0; I <arr1.length; i++) {Console.log (arr1[i])}</script>
<script type= "Text/javascript" >//merging concat of arrays        //var North =["Henan", "Beijing", "Hebei";        //var South =["Shanghai", "Suzhou", "Hangzhou";        //var newArr = North.concat (south);        //Console.log (NEWARR)        //Convert to String toString () directly to string, separated by commas        varscore=[100,33,56,67,87]; varstr =score.tostring (); Console.log (str)//Join () method, using the specified string to split the array        varSTR2 = Score.join ("|"); Console.log (STR2)//indexOf () checks the subscript of a specified string, referring to a forward lookup        varindex = Score.indexof (67); varIndex1 = Score.indexof (99);//the lookup is not in the array, returns-1Console.log (Index) console.log (INDEX1)//lastindexOf (), flashback lookup. Note the index values for forward and reverse lookups are the same.        varIndex2 =score.lastindexof (67); Console.log (INDEX2)//Array of arrays to be sorted, and the original array will be reversed        varNames =["Alne", "Jack", "Chen", "Wang", "Liu", "Ago"]; varReversename =Names.reverse (); Console.log (Reversename) console.log (names)//in alphabetical order, if the first character is the same, in the following letter, and so on        varNames1 =Names.sort (); Console.log (names1)//removes the first element.        varFrist =Names.shift (); Console.log (frist) Console.log (names)//Unshift () adds one or more values and returns the length of the array.        varNames2 = Names.unshift ("Pinjin", "Xuehua", "Xiaohei"); Console.log (names) Console.log (names2)//push (), pop () Add, delete        //push () is added by default to the end of the array        varNames3 = Names.push ("Lirui", "Xiaoxiao"); Console.log (names) Console.log (NAMES3)//returns the new length        //pop () Delete defaults from the last start Delete, names4 is the deleted element        varNames4 =Names.pop ();        Names.pop ();        Names.pop (); Console.log (NAMES4)//split, invert string "Hello Luffy"        varA = "Hello Luffy"; //var x = A.split ("")Console.log (A.split (""). Reverse ())</script>
common methods for arraysfunction:
<script type= "Text/javascript" >//JS in the function is declared to be with function funcname (), there are functions of the Declaration, there must be a call.        functionAdd () {alert ("The function is called.")} Add ()//declaring a function with formal parameters        functionadd2 (x, y) {alert+y)} add2 (4,7)        //function with return value        functionAdd3 (A, b) {returnA *B}//Console.log (ADD3 (5,9))Alert (ADD3 (5,10))    </script>

js02-Common Flow control statements

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.