Nested Loops

390阅读 1评论2013-12-26 wuxiaobo_2009
分类:LINUX


点击(此处)折叠或打开

  1. 1.2. Nested Loops

  2. A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.

  3. Example 11-19. Nested Loop

  4. #!/bin/bash
  5. # nested-loop.sh: Nested "for" loops.

  6. outer=1 # Set outer loop counter.

  7. # Beginning of outer loop.
  8. for a in 1 2 3 4 5
  9. do
  10.   echo "Pass $outer in outer loop."
  11.   echo "---------------------"
  12.   inner=1 # Reset inner loop counter.

  13.   # ===============================================
  14.   # Beginning of inner loop.
  15.   for b in 1 2 3 4 5
  16.   do
  17.     echo "Pass $inner in inner loop."
  18.     let "inner+=1" # Increment inner loop counter.
  19.   done
  20.   # End of inner loop.
  21.   # ===============================================

  22.   let "outer+=1" # Increment outer loop counter.
  23.   echo # Space between output blocks in pass of outer loop.
  24. done
  25. # End of outer loop.

  26. exit 0

上一篇:Chapter 11. Loops and Branches --11.1 Loops
下一篇:不积跬步,无以至千里。一天做好三件:

文章评论