home *** CD-ROM | disk | FTP | other *** search
/ Chip 2008 June / CHIP-2008-06.iso / software / PowerShell / PowerShell_Setup_x86.msi / product.cab / about_break.help_EN.txt < prev    next >
Encoding:
Text File  |  2007-10-29  |  4.5 KB  |  136 lines

  1. TOPIC
  2.     The break statement
  3.  
  4. SHORT DESCRIPTION
  5.     A statement for immediately exiting foreach, for, while, do, or switch 
  6.     statements
  7.  
  8. LONG DESCRIPTION
  9.     A break statement appearing in a code block of a foreach, for, while, 
  10.     or do loop or in a switch statement, ends the code block. In the case 
  11.     of the looping statements, the break statement causes the loop to exit. 
  12.     In the case of the switch statement, the break statement causes a code 
  13.     block inside of a switch statement to exit and thus the entire switch 
  14.     statement exits.
  15.  
  16.     The following example shows how to use a break statement to exit a for 
  17.     statement:
  18.  
  19.         for($i=1; $i -le 10; $i++)
  20.         {
  21.             Write-Host $i
  22.             break
  23.         }
  24.  
  25.     In this case, the break statement exits the for loop when $i equals 1. 
  26.     Even though the for statement evaluates to true until $i is greater 
  27.     than 10, the Windows PowerShell reaches the break statement 
  28.     the first time through the for loop. 
  29.  
  30.     It's more common to see the break statement used inside of a loop where 
  31.     there is some inner condition to be met. Consider the following foreach 
  32.     statement example:
  33.  
  34.         $i=0
  35.         $varB = 10,20,30,40
  36.         foreach ($val in $varB)
  37.         {    
  38.             $i++
  39.             if ($val -eq 30)
  40.             {
  41.                 break
  42.             }    
  43.         }
  44.         Write-Host "30 was found in array position $i"
  45.  
  46.     In this example, the foreach statement iterates the $varB array. Each 
  47.     time through the code block, the $i variable is incremented by 1. The 
  48.     if statement inside evaluates to false the first two times through the 
  49.     loop. The third time through the loop, $i equals 3 and the $val 
  50.     variable equals 30, at which point the break statement runs and the 
  51.     foreach loop exits.
  52.  
  53.     Breaking out of the other looping statements is identical to how you 
  54.     break out of the foreach loop. In the following example, the break 
  55.     statement exits a while statement when a DivideByZeroException is 
  56.     trapped using the trap statement.
  57.  
  58.         $i = 3
  59.         while ($true)
  60.         {
  61.             trap [DivideByZeroException] 
  62.             {
  63.                 Write-Host "divide by zero trapped" 
  64.                 break
  65.             }
  66.             1 / $i--
  67.         }
  68.  
  69.     A switch statement is not a looping construct, but the break statement 
  70.     is useful for ending the program flow when a particular condition is 
  71.     met. For example, the following switch statement uses break statements 
  72.     to test for the most specific condition:
  73.  
  74.         $var = "word2"
  75.         switch -regex ($var) 
  76.         {
  77.             "word2" 
  78.             {
  79.                 Write-Host "Exact" $_ 
  80.                 break
  81.             } 
  82.  
  83.             "word.*" 
  84.             { 
  85.                 Write-Host "Match on the prefix" $_ 
  86.                 break
  87.             }
  88.  
  89.             "w.*"
  90.             {
  91.                 Write-Host "Match on at least the first letter" $_
  92.                 break 
  93.             }
  94.             
  95.             default
  96.             {
  97.                 Write-Host "No match" $_
  98.                 break
  99.             }
  100.         }
  101.  
  102.     In this example, $var is created and initialized to a string value of 
  103.     "word2". The switch statement uses regex (a regular expression .NET 
  104.     class) to match the variable value first with the term "word2". Because 
  105.     the variable value and the first test in the switch statement match, 
  106.     the first code block in the switch statement runs. When PowerShell reaches 
  107.     the first break statement, the switch statement exits. If the four break 
  108.     statements were removed from the example, then all four conditions 
  109.     would be met. Thus, this example uses the break statement to display 
  110.     results when the most specific condition is met. 
  111.  
  112.  
  113. SEE ALSO
  114.     For information about using comparison operators, enter the following 
  115.     command at the PowerShell command prompt:
  116.          
  117.         help about_comparison_operators
  118.  
  119.     For information about the switch statement, enter the following 
  120.     command:
  121.  
  122.         help about_switch
  123.  
  124.     For information about the foreach statement, enter the following 
  125.         command:
  126.  
  127.         help about_foreach
  128.  
  129.     For information about the for statement, enter the following command:
  130.  
  131.         help about_for
  132.  
  133.     For information about the while statement, enter the following command:
  134.  
  135.         help about_while
  136.