Please enable JavaScript.
Coggle requires JavaScript to display documents.
If, For, While statements (While statements (Possible commands (Continue…
If, For, While statements
-
For statements:
Tip
If you are looping through a list etc, do not add new item to the end of the list as you are running through the loop. It will result in an infinite loop
-
-
While statements
Basic structure
i = 0 While i < 5: i+=1 Rmb that you have to defined i first ON TOP of the while statement and you have to increase i to ensure that the CONDITION IS MET AT A CERTAIN POINT or it will just result in an infinite loop
Possible commands
Continue This will skip the execution of the code below inside the while loop and immediately go to the first line of the current while loop. Rmb you have to call i+=1 or equivalent statement before continue! Continue does NOT auto increase i by itself!
Break. This will skip the while loop entirely and immediately go to the statement AFTER the entire while loop
Else: This statement will only be executed after the while loop is complete AND NO break happened! Eg. While i < 5: i+=1 Else: print('Done!')
While True: will loop infinitely as the statement if always true. While False: will not execute at all
Tip
While statements CANNOT be used to iterate through lists/set/tuple/strings/dictionary etc using While i in string:! Use FOR statements instead