While General syntax
While control condition: EXECUTE statement
The execution statement is executed until the control condition is false.
• Infinite Loops
While True: Execute statement
It is very dangerous to be able to execute the process all the time, and we will only see them on certain occasions. For example, some servers need to listen to a client every minute of the time to connect. These clients may make requests to the server at any time, so it is necessary for the server's listener to remain on standby.
• Counting Cycles
This is the most common statement that uses counters to determine when a loop terminates.
Count = 0;while Count <: Print count count + = 1
The range () function requires one, two, or three parameters to be supplied
Range (start, end, step)
If step 1 can be omitted, there are two parameters at this time
If there is only one parameter, the default start counts from 0
The Len () function returns the number of elements of a sequence
For loop
Movies = ["Avata", "Xman", "Titannic", "The Dark Knight Rises", "Jurassic Park", "Dances with Wolves"]for movie in Movies: print Moviefor item in range (Len (Movies)): Print Item, ":", Movies[item]
Break&continue as in C, break is breaking out of this layer, while continue is not continuing to execute the following code, jumping back to loop to perform the next loop
For I in range: if i = = 6: Break Print i,for i in range: if i = = 6: Continue Print I,
Python Learning Notes (iv)