Web Development (beginner)-Python, JavaScript, and jquery loop statements

Source: Internet
Author: User

Looping StatementsI. Overview

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

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

jquery Loop Statements each

Second,pythonLooping Statements


2.1 For Loop

# A, Li = [1, 2, 3, 4]for I in Li:print (i)
# b, Li = [1, 2, 3, 4]for I, J in enumerate (Li, 1): Print (I, J) # Enumerate (li, 1) 1 for index starting from 1, default is null, represents starting from 0
# c, li1 = [1, 3, 5, 7]li2 = [2, 4, 6, 8]for I, J in Zip (Li1, li2): Print (i, j)
# d, do not cycle the dictionary through Dic.items (), the efficiency will be very low dic = {' A ': 1, ' B ': 2}for K in Dic:print (K, Dic.get (k)) # The above code, the equivalent of a dictionary key to the loop, equivalent to the next Face code: dic={' A ': 1, ' B ': 2}for K in Dic.keys (): Print (K, dic.get (k)) # for the value of the loop, that is, for the V in Dic.values () ...

2.2 While Loop
While True:pass # in Python except for none, ', [], {}, (), False, the others are true, that is. # for cyclic judgment: eg. flag# determines if true: while flag:# determines whether it is false: While not flag:
third, JavaScript loop statementA, while loop
var count = 0;while (Count < Ten) {Console.log (count); Count + +;} # JavaScript defines local variables with Var
B, Do/while
do{code block;} while (conditional statement)
C, for
var a = document.getElementById (' key '). 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 '); Get the label for all type= ' text ' under id= ' key ' 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].ch  Ecked=false;  }else{c1[i].checked=true; }}
Iv. jquery Loop Statements

Each statement:

$ (': Text '). each (function () {Console.log ($ (this). Val ());}); # $ (': Text ') ==> $ (' input[type= "text"] ')

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

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

Jump out of the loop with return in jquery:

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

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





Web Development (beginner)-Python, JavaScript, and jquery loop statements

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.