You may not know the differences between java, python, JavaScript, and jquary loop statements, pythonjquary

Source: Internet
Author: User

You may not know the differences between java, python, JavaScript, and jquary loop statements, pythonjquary
I. Overview

Java loop statements are divided into four forms: while, do/while, for, and foreach;

There are two types of loop statements in python: while,;

There are four types of loop statements in JavaScript: while, do/while, for, for/in

JQuery loop statement each

Ii. java loop Statement a and while

While Syntax:

While (Condition Statement) {code block}

Or:

While (Condition Statement) code;

The meaning of while is very simple. as long as the condition statement is true, it will always execute the following code. If it is false, it will stop. 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, the system always prompts that reader. nextInt () receives a number from the screen.

B. do/while

No matter what the conditional statement is, the code block will be executed at least once, you can use the do/while loop. The do/while syntax is:

Do {code block;} while (Condition Statement)

That is, execute the code block first, and then determine whether the condition is true. If the condition is true, continue to execute the Code. If the condition is not true, exit the 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 is applicable to situations where the number of loops is known. syntax rules:

For (initialization statement; cyclic condition; step-by-step operation) {cyclic body}

Each time you determine the cycle condition, the condition is set up and the execution cycle is completed, the initial value is used for step-by-step operations. 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 the length of 4 in arr, the loop is executed. Note that after the loop is executed, I = 4, that is, although no loop is executed, I increases by 1.

When the initial value is null:

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(;;){}

Is effective. This is an endless loop, but nothing is done every time. It is equivalent to executing the pass statement every time in python.

D, Foreach

The syntax of foreach is as follows:

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

Foreach uses the colon: each element in the loop is preceded by the colon, including the data type and variable name. The colon is followed by the array or set to be traversed, And the element is automatically updated each cycle.

E. Loop Control:

Break; jumps out of the cycle of the current layer.

After the break is executed, no operations in the loop will be executed, and the initial value will not be auto-incremented.

Continue; jumps out of this loop, the initial value is auto-incrementing, And the next loop is executed.

Iii. python circular statement 3.1 for Loop

A,

li=[1,2,3,4]for i in li:    print(i)

In the above code, I represents every element of the List li. The syntax 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 every element of li.

Note: The index starts from 0 by default. You can set for I, j in enumerate (li, 1): To set the index to start 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 li1 element in the list, and j Represents the li2 element.

D,

dic={'a':1,'b':2}for k in dic:    print(k)

In the above Code, it is equivalent to repeating the dictionary key and 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 dictionary values.

F,

dic={'a':1,'b':2}for k,v in dic.items():    print(k,v)

In the above Code, k Represents the dictionary key, and v represents the dictionary value.

3.2 while LOOP

A,

i=1while i:    pass

In the above Code, while I: executes a loop when I is the true value. In python, except for none, empty string, empty list, empty dictionary, empty tuples, and False, all others are the true values, true.

B,

while True:    pass

The above Code applies to an infinite loop, that is, the condition is true by default.

C. Other general while loops:

While condition: pass

In my experience, if you need to use a condition that is already false as a loop condition in python, you can use the following solution:

Solution 1,

i=Falsewhile i is not True:    pass

Or:

i=Falsewhile i is False:    pass

Solution 2,

i=Falsewhile i ==False:   pass
Iv. JavaScript loop Statement a and while LOOP
var cont=0;while(cont<10){       console.log(cont);       cont++;}

The code above shows that the JavaScript while LOOP requires an initial value first. Each time the cycle condition is determined, the condition is true, and the initial value is automatically increased within the loop.

If you want to generate an endless loop, you can change the above Code:

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

In this case, you do not need to set the initial value and auto-increment value.

B. do/while

In JavaScript, do/while is the same as do/while in java. For details, refer to java do/while in the above section. Note that var is used to define variables in JavaScript.

Do {code block;} while (Condition Statement)

That is, execute the code block first, and then determine whether the condition is true. If the condition is true, continue to execute the next loop. If the condition is not true, exit the loop.

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 obtain all the type = 'text' labels under id = 'k1 'and set the value 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 code above finds all input labels and loops them. Here I represents the index. The code above operates on the check box that is deselected. If it is selected, select checked = false for this label. Otherwise, set it to true;

V. jQuery loop statements

Each statement:

$(':text').each(function(){           console.log($(this).val()) ;        });

Syntax Rules: tag set. each (anonymous function );

The code above means: Get the type = 'text' tag in all the indium tags, and cycle it to print its value each time.

Return:

Return ture: exit this loop and execute the next loop, which is equivalent to the continue of other languages;

Return false: exit the current layer loop, that is, exit the current each, which is equivalent to the break of other languages;

 

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.