1. Overview
There are two types of loop statements in python, while and for;
There are four types of loop statements in JavaScript: while, do/while, for, and for/in
jQuery loop statement each
2. Java loop statement
a. while
The syntax of while is:
while(conditional statement){code block}or:
while (conditional statement) code;
The meaning of while is very simple. As long as the conditional statement is true, the subsequent code will be executed all the time, and if it is false, it will stop doing it. For example:
Scanner reader = new Scanner(System.in);System.out.println("please input password");int num = reader.nextInt();int password = 6789;while(num!=password){ System.out.println("please input password"); num = reader.nextInt();}System.out.println("correct");reader.close();In the above code, as long as the password is not equal to 6789, it is prompted to enter, and reader.nextInt() receives a number from the screen.
b, do/while
Regardless of the conditional statement, the code block will be executed at least once, and you can use a do/while loop. The syntax of do/while is:
do{ code block;}while(conditional statement) That is: first execute the code block, then determine whether the condition is true. If it is true, continue to execute the code, and the exit loop is not true.
Scanner reader = new Scanner(System.in);int password = 6789;int num = 0;do{ System.out.println("please input password"); num = reader.nextInt();}while(num!=password);System.out.println("correct");reader.close();c. for loop
For loops are suitable for cases where the number of loops is known. Syntax rules:
for(initialization statement; loop condition; step operation){ loop body} Each time the loop condition is judged, the condition holds the execution loop, and after the execution is completed, the initial value is performed stepping operation. Sample code:
int[] arr = {1,2,3,4};for(int i=0;i<arr.length;i++){ System.out.println(arr[i]);} As long as i is less than arr's length 4, the loop is executed. It should be noted that after the loop is executed, i=4, that is, although the loop is not executed, i is increased by 1.
In case of empty initial value:
int[] arr = {1,2,3,4};int i=0;for(;i<arr.length;i++){ System.out.println(arr[i]);}This is because the initial value is defined before the loop.
In for, each statement can be empty, that is:
for(;;){} It is valid, this is a dead loop, but I don't do anything every time, which is equivalent to executing a pass statement every time in python.
d, foreach
The syntax of foreach is shown in the following code:
int[] arr = {1,2,3,4};for(int element : arr){ System.out.println(element);} Foreach uses colon: , which is preceded by each element in the loop, including the data type and variable name, and is followed by the array or collection to be traversed. Each loop element will be automatically updated.
e. Cycle control:
break; break; break out of this loop.
After performing break, no operation of the loop is performed, and the initial value will not increase itself.
continue; jump out of this loop, the initial value increases by itself, and the next loop is executed.
3. Python loop statements
3.1 for loop
a.
li=[1,2,3,4] for i in li: print(i)
In the above code, i represents each element of the list li. The syntax rule is for ...in ..., which is equivalent to foreach in java.
b.
li=[1,2,3,4] for i,j in enumerate(li): print(i,j)
In the above code, i represents the index of the list li, and j represents each element of li.
Note: The index starts from 0 by default, you can set for i,j in enumerate(li, 1): thus set the index starts from 1.
c.
li1=[1,2,3,4]li2=[4,5,6,7] for i,j in zip(li1,li2): print(i,j)
In the above code, i represents the element of list li1 and j represents the element of li2.
d.
dic={'a':1,'b':2} for k in dic: print(k)In the above code, it is equivalent to looping the key of the dictionary, which is equivalent to the following code:
dic={'a':1,'b':2}for k in dic.keys(): print(k)e,
dic={'a':1,'b':2}for k in dic.values(): print(k)In the above code, it is equivalent to looping the values of the dictionary.
f,
dic={'a':1,'b':2}for k,v in dic.items(): print(k,v) In the above code, k represents the key of the dictionary, and v represents the value of the dictionary.
3.2 while loop
a.
i=1while i: pass
In the above code, while i: that is, when i is the true value, the loop is executed. In python, except none, empty string, empty list, empty dictionary, empty tuple, and False, all others are true values, that is, True.
b.
While True: pass
The above code is suitable for a dead loop, that is, the condition defaults to true.
c. Other general while loops:
While conditions: pass
According to my experience, if the condition that has already false is needed as a loop condition in python, the following scheme can be adopted:
Plan 1.
i=Falsewhile i is not True: pass
or:
i=Falsewhile i is False: pass
Plan II.
i=Falsewhile i ==False: pass
4. JavaScript loop statements
a. While loop
var cont=0;while(cont<10){ console.log(cont); cont++;} The above code shows that the JavaScript while loop needs to first give the initial value, and each time the loop condition is judged, the loop is executed if the condition is true, and the initial value is increased automatically within the loop.
If you want to generate a dead loop, the above code can be changed to:
while(true){ console.log(1); }At this time, there is no need to set the initial value and self-increment.
b, do/while
Do/while in JavaScript is the same as do/while in Java, refer to java do/while in the above article. It is necessary to note that var is used to define variables in JavaScript.
do{ code block;}while(conditional statement)That is, first execute the code block, then determine whether the condition is true. If the condition is true, continue to execute the next loop, and the exit loop is not true.
c.
var a=document.getElementById('k1').children;for(var i=0;i< a.length;i++){ var inp=a[i]; var at=inp.getAttribute('type'); if (at=='text')inp.setAttribute('value','123');}The above code is to get all the tags of type='text' under id='k1' and set the value equal to '123'.
d, for in
var c1=document.getElementById('i1').getElementsByTagName('input'); for(var i in c1){ if(c1[i].checked)c1[i].checked=false; else c1[i].checked=true; } The above code is to find all input tags and loop them. Here i represents the index. The above code operates on the reverse check box. If it is selected, select checked=false for the tag, otherwise, set to true;
5. jQuery loop statement
Each statement:
$(':text').each(function(){ console.log($(this).val()) ; });Syntax rules: tag collection.each (anonymous function);
The above code means: get the type='text', tags in all inp tags, and loop them, and print their value each time.
Use return to jump out of the loop in jQuery:
return truth: Exit this loop and execute the next loop, which is equivalent to the continue of other languages;
return false: Exit this layer loop, that is, exit the current each, which is equivalent to break in other languages;
The above is a collection of information on Java, python, Javascript and jquary loop statements. Friends who need it can refer to it.