Javascript process control

Posted by heckenschutze on Wed, 06 May 2020 00:42:00 +0200

Javascript process control

1. Conditional statement

(1)if(exp) executes a code

(2)if(exp) {execute code segment;}

(3)if(exp){exp is true to execute code segment} else{exp is false to execute code segment}

  (4)if...else if...

(5)if nesting

2. Loop statement

(1)for cycle

(2)while cycle

(3)do/while cycle

3. Special cycle control

(1)break terminate cycle

(2)continue skip cycle

Here are the details:

for loop

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <h3>for loop</h3>
 9     <p>for(exp1;exp2;exp3;){Circulatory body;}</p>
10     <p>exp1:Execute the first statement unconditionally</p>
11     <p>exp2:Judgment is the condition that euroserver can execute the circulatory body</p>
12     <p>exp3:Do incremental operations</p>
13     <script>
14         for(var i=0;i<4;i++){
15             document.write(i+'hello world <br />')
16         }
17         // loop i++,i Cycle three times, output several times each time+hello world
18     </script>
19     <p>adopt break End cycle</p>
20     <script>
21         for(var i=0;i<=6;i++){
22             if(i>5){
23                 break;
24             }
25             document.write(i+'<br/>');
26         }//loop for Statement output 1 at a time i Value, when i>5 Stop output when
27     </script>
28     <p>adopt continue Skip grade cycle</p>
29     <script>
30         for(var i=1;i<=6;i++){
31             if(i==5){
32                 continue;
33             }
34             document.write(i+'<br />')
35         }//loop for Statement, output 1 at a time i The one worth it i=5 Skip this cycle to enter the next cycle.
36     </script>
37 </body>
38 </html>

for loop nesting

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <h3>for loop nesting </h3>
 9     <script>
10         for(var i=1;i<=3;i++){
11             for (var k=1;k<=2;k++){
12                 document.write(k);
13             }
14             document.write(i+'<br>');
15         }
16     </script>
17 </body>
18 </html>

Conditional statement

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 < H3 > conditional statement</h3>
 9 < p > if 3 > = 11, output a; otherwise, output B</p>
10     <script>
11         if(3>=11){
12             document.write('a');
13         }else{
14             document.write('b');
15         }
16     </script>
17 < p > define a=1, judge whether a is all equal to 2, if it is all equal, output 2; if it is not all equal, judge whether a is all equal to 3, if it is all equal, output 3; if it is not all equal, judge whether a is all equal to 1, if it is all equal, output 1</p>
18     <script>
19         var a=1;
20         if(a==2){
21             document.write(2);
22         }else if(a==3){
23             document.write(3);
24         }else if(a==1){
25             document.write(1);
26         }
27     </script>
28 </body>
29 </html>

switch loop

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <h3>switch loop</h3>
 9     <p>switch If the specified value is not found in the loop, it will be compared all the time. When the specified value is found, all the code will be executed. Therefore, in the swith The code segment of each value in the loop should be written at the end break</p>
10     <p>No addition break Front</p>
11     <script>
12         var i=3;
13         switch(i){
14             case 1:document.write('a<br />');
15             case 2:document.write('b<br />');
16             case 3:document.write('c<br />');
17             case 4:document.write('d<br />');
18         }//Because we didn't join here break So when we find out i=3 Execute all code after
19     </script>
20     <p>plus break after</p>
21     <script>
22         switch(i){
23             case 1:document.write('a<br />');break;
24             case 2:document.write('b<br />');break;
25             case 3:document.write('c<br />');break;
26             case 4:document.write('d<br />');break;
27         }
28     </script>
29 </body>
30 </html>

while loop

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <h3>while loop</h3>
 9     <script>
10         var i=0;
11         while(i<=5){
12             i++;
13             document.write(i+'<br />');
14         }//if i<5 Then execute i++And output i value
15     </script>
16 </body>
17 </html>

do/while loop

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <h3>do/while loop</h3>
 9     <script>
10         var x=0;
11         do{
12             x++;
13             document.write('The first'+x+'second X The value of is:'+x+'<br>')
14         }
15         while (x<4);
16         document.write('Final X The value of is:'+x+'<br />')
17         //When x<4 The output of the x Value of,
18     </script>
19 </body>
20 </html>

Topics: Javascript