parm530
10/15/2019 - 2:20 PM

Loops

#
# Example file for working with loops
#

def main():
  x = 0

  # define a while loop
  while(x < 5):
    print(x)
    x = x + 1 


  # define a for loop
  for x in range(5, 10):
    range from 5 to 10, NOT INCLUDING 10
    print(x)


  # use a for loop over a collection
  days = ["Mon", "Tues", "Wed", "Thurs", "Fri"]
  for day in days:
    print(day)
    
 
  # use the break and continue statements
  for x in range(5, 10):
    # if (x == 7): break # breaks out of the loop, example: never executes for 7, 8, 9
    if (x % 2 == 0): continue # skip the rest of the execution of the code, go back to the top, ex. don't print x
    print(x)

  #using the enumerate() function to get index 
  days = ["Mon", "Tues", "Wed", "Thurs", "Fri"]
  for i,day in enumerate(days):
    # Access the index counter by using the enumerate function on the array
    print(i, day)
    
if __name__ == "__main__":
  main()