5 循环语句

5.1 while 循环

  • while 循环的格式
      while 条件:
          条件满足时,做的事情1
          条件满足时,做的事情2
          条件满足时,做的事情3
          ...(省略)...
    

5.2 for 循环

  • for 循环的格式
      for 临时变量 in 列表或者字符串等可迭代对象:
          循环满足条件时执行的代码
    
  • demo1

      name = 'itheima'
    
      for x in name:
          print(x)
    

    运行结果如下:

      i
      t
      h
      e
      i
      m
      a
    
  • demo2

      for x in name:
          print(x)
          if x == 'l':
              print("Hello world!")
    

    运行结果如下:

      h
      e
      l
      Hello world!
      l
      Hello world!
      o
    

5.3 break 和 continue

5.3.1 break

  • for循环

    • 普通的循环示例如下:
      name = 'itheima'
      for x in name:
          print('----')
          print(x)
      else:
          print("==for循环过程中,如果没有执行break退出,则执行本语句==")
      
      运行结果:
      ----
      i
      ----
      t
      ----
      h
      ----
      e
      ----
      i
      ----
      m
      ----
      a
      ==for循环过程中,如果没有break则执行==
      
    • 带有break的循环示例如下:
      name = 'itheima'
      for x in name:
          print('----')
          if x == 'e': 
              break
          print(x)
      else:
          print("==for循环过程中,如果没有执行break退出,则执行本语句==")
      
      运行结果:
      ----
      i
      ----
      t
      ----
      h
      ----
      
  • while循环

    • 普通的循环示例如下:
      i = 0
      while i<5:
          i = i+1
          print('----')
          print(i)
      else:
          print("==while循环过程中,如果没有执行break退出,则执行本语句==")
      
      运行结果:
      ----
      1
      ----
      2
      ----
      3
      ----
      4
      ----
      5
      ==while循环过程中,如果没有break则执行==
      
    • 带有break的循环示例如下:
      i = 0
      while i<5:
          i = i+1
          print('----')
          if i==3:
              break
          print(i)
      else:
          print("==while循环过程中,如果没有执行break退出,则执行本语句==")
      
      运行结果:
      ----
      1
      ----
      2
      ----
      

5.3.2 continue

  • for循环

    • 带有continue的循环示例如下:
      name = 'itheima'
      for x in name:
          print('----')
          if x == 'e': 
              continue
          print(x)
      else:
          print("==while循环过程中,如果没有break则执行==")
      
      运行结果:
      ----
      i
      ----
      t
      ----
      h
      ----
      ----
      i
      ----
      m
      ----
      a
      ==while循环过程中,如果没有break则执行==
      
  • while循环

    • 带有continue的循环示例如下:
      i = 0
      while i<5:
          i = i+1
          print('----')
          if i==3:
              continue
          print(i)
      
      运行结果:
      ----
      1
      ----
      2
      ----
      ----
      4
      ----
      5
      

5.3.3 小结

  • continue的作用:用来结束本次循环,紧接着执行下一次的循环
  • 循环可以和else配合使用,else下方缩进的代码指的是当循环正常结束之后要执行的代码
  • 所谓else指的是循环正常结束之后要执行的代码,即如果是break终止循环的情况,else下方缩进的代码将不执行
  • 因为continue是退出当前一次循环,继续下一次循环,所以该循环在continue控制下是可以正常结束的,当循环结束后,则执行了else缩进的代码
  • 注意点:
    • break/continue只能用在循环中,除此以外不能单独使用
    • break/continue在嵌套循环中,只对最近的一层循环起作用
    • else
      • while和for都可以配合else使用
      • else下方缩进的代码含义:当循环正常结束后执行的代码
      • break终止循环不会执行else下方缩进的代码
      • continue退出循环的方式执行else下方缩进的代码

5.4 循环嵌套

  • while 循环嵌套格式
      while 条件1:
          条件1满足时,做的事情1
          条件1满足时,做的事情2
          条件1满足时,做的事情3
          ...(省略)...
          while 条件2:
              条件2满足时,做的事情1
              条件2满足时,做的事情2
              条件2满足时,做的事情3
              ...(省略)...
    
  • for 循环嵌套格式
      for 临时变量 in 列表或者字符串等可迭代对象:
          循环满足条件时执行的代码
          for 临时变量 in 列表或者字符串等可迭代对象:
              循环满足条件时执行的代码
    

results matching ""

    No results matching ""