Differences in Java, Python, JavaScript, and jquery looping statements _java

Source: Internet
Author: User
Tags in python

I. Overview

There are two kinds of circular statements in Python, while,for;

There are four kinds of looping statements in JavaScript, while,do/while,for,for/in

  jquery Loop Statements each

Second, Java Loop statement

A, while

The while syntax is:

while (conditional statement) {
 code block
}

Or:

while (conditional statement) code;

While the meaning is very simple, as long as the conditional statement is true, the following code has been executed, for false stop do not do. 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 code above, as long as the password is not equal to 6789, always prompt input, reader.nextint () from the screen to receive a number.

B, Do/while

No matter what the conditional statement is, the code block executes at least once, and you can use the Do/while loop. The syntax for Do/while is:

do{
 code block;
while (conditional statement)

That is: The first execution code block, and then judge whether the condition is established, if set up, continue to execute code, do not set up exit loop.

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

The For loop applies to cases where the number of loops is known, and the syntax rules:

for (initialization statement; loop condition; stepping operation) {
 loop body
}

Each judgment cycle condition, the condition establishes executes the loop, after completes, the initial value carries on the step operation, the example 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 the length of arr 4, to execute the loop, it should be noted that after the loop execution, i=4, that is, although not the execution loop, but I since the increase of 1.

The initial value is empty:

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 to say:

for (;;) {}

is valid, it's a dead loop, but it doesn't do anything every time, equivalent to every pass statement in Python.

D, foreach

The foreach syntax is shown in the following code:

Int[] arr = {1,2,3,4};
for (int element:arr) {
 System.out.println (element);
}

foreach uses a colon: the colon is preceded by each element in the loop, including the data type and variable name, followed by the array or collection to traverse, and each loop element is automatically updated.

E, circular control:

Break, jump out of this layer of cycle.

After the break is executed, no more loops are performed, and the initial value does not increase by itself.

Continue, jump out of this cycle, the initial value of the increase, the next cycle.

Three, Python loop statement

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 ..., the equivalent of a 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 every element of li.

Note: The index defaults from 0 and can be set for i,j in Enumerate (li,1): thus setting the index starting at 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 the list Li1, and J represents the element of Li2.

D

Dic={' A ': 1, ' B ': 2} for
K in dic:
 print (k)

The code above, which is equivalent to looping the key of the dictionary, 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 code above, the equivalent of the dictionary values is recycled.

F

Dic={' A ': 1, ' B ': 2} for
k,v in Dic.items ():
 print (K,V)

In the above code, K represents the dictionary's key,v representing the value of the dictionary.

3.2 While loop

A

I=1 while
I:
 Pass

In the preceding code, while I: the loop executes when I is true, in Python, except none, empty strings, empty lists, empty dictionaries, empty tuples, False, and all others are truth values.

B

While True: Pass
 

The preceding code applies to a dead loop, where the condition defaults to true.

C, other universal while loops:

While condition: Pass
 

As far as I'm experienced, if you need to use false conditions as a cyclic condition in Python, you can take the following scenarios:

Programme I,

I=false while
I isn't True: pass
 

Or:

I=false while
I was False:
 Pass

Programme II,

I=false while
i ==false:
 Pass

Four, JavaScript loop statement

A, while loop

var cont=0;
while (cont<10) {
 console.log (cont);
 cont++;
}

The code above shows that the JavaScript while loop is preceded by a given initial value, each time the loop condition is evaluated, the condition is a true execution loop, and the initial value is amplified within the loop.

If you want to generate a dead loop, the code above can be changed to:

while (true) {
 console.log (1);
 }

At this point, you do not need to set the initial value and the self increase.

B, Do/while

In JavaScript do/while and Java Do/while, refer to the Java do/while above. Note that you define variables in JavaScript with var.

do{
 code block;
while (conditional statement)

That is, the first execution of the code block, and then judge whether the condition is established, the establishment will continue to perform the next cycle, not set up exit cycle.

C, for

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 the label of all 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 it, where I represents the index, the above code operation is the reverse check box, if selected, choose to set the label of the Checked=false, on the contrary, set to true;

Five, jquery loop statement

Each statement:

$ (': Text '). each (the function () {
 Console.log ($ (this). Val ());
 });

Syntax rules: A collection of tags. each (anonymous function);

The above code means: Get all the tags in the INP label type= ' text ', and loop it, printing its value each time.

jquery jump out of loop with return:

return ture: Exit this cycle, perform the next cycle, equivalent to the continue of other languages;

return false: Exits this layer loop, that is, exiting the current each, equivalent to a break in another language;

This is the Java, Python, JavaScript and jquary circular statements of data collation, the need for friends can refer to.

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.