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

  1. TOPIC
  2.     The if statement
  3.  
  4. SHORT DESCRIPTION
  5.     A language command for running a command block or blocks based on the 
  6.     results of one or more conditional tests
  7.  
  8. LONG DESCRIPTION
  9.     You can use the if statement to run a code blocks if a specified 
  10.     conditional test evaluates to true. You can also specify one or more 
  11.     additional conditional tests to run if all prior tests evaluate to 
  12.     false. Finally, you can specify an additional code block that is run if 
  13.     no other prior conditional test evaluates to true. 
  14.  
  15.     The following shows the if statement syntax:
  16.  
  17.         if (<test1>) 
  18.             {<code_block1>}
  19.         [elseif (<test2)
  20.             {<code_block2>}]
  21.         [else
  22.             <code_block3>}]
  23.  
  24.     When you run an if statement, the Windows PowerShell evaluates the 
  25.     <test1> conditional expression true or false. If <test1> is true, 
  26.     then <code_block1> runs and PowerShell exits the if statement. 
  27.     If <test1> is false, PowerShell evaluates the condition specified by 
  28.     <test2>. If <test2> is true, then <code_block2> runs and PowerShell 
  29.     exits the if statement. If both <test1> and <test2> evaluate to false, 
  30.     then <code_block3> runs and PowerShell exits the if statement. 
  31.  
  32.     You can use multiple elseif statements to chain a series of conditional 
  33.     tests, each of which is examined only if all the previous tests are 
  34.     false. Note, if you need to create an if statement containing many 
  35.     elseif statements within it, consider using a switch statement instead.
  36.  
  37.     The simplest if statement contains a single command in the code block 
  38.     and does not contain any elseif statements or an else statement. The 
  39.     following shows the simplest form of the if statement:
  40.  
  41.         if ($a -gt 2)
  42.         {
  43.             Write-Host "The value $a is greater than 2"
  44.         }
  45.  
  46.     In this example, if $a is greater than 2, the condition evaluates to 
  47.     true and the code block runs. However, if $a is less than or equal to 2 
  48.     or is not an existing variable, the if statement does not display any 
  49.     message. By adding an else statement, you can be sure to display a 
  50.     message when $a is less than or equal to 2, as the next example shows:
  51.  
  52.         if ($a -gt 2)
  53.         {
  54.             Write-Host "The value $a is greater than 2"
  55.         }
  56.         else
  57.         {
  58.             Write-Host "The value $a is less than or equal to 2, is not 
  59.         created or is not initialized."
  60.         }
  61.  
  62.     To further improve on this example, you can use the elseif statement to 
  63.     display a message when the value of $a is equal to 2, as the next 
  64.     example shows:
  65.  
  66.         if ($a -gt 2)
  67.         {
  68.             Write-Host "The value $a is greater than 2"
  69.         }
  70.         elseif ($a -eq 2)
  71.         {
  72.             Write-Host "The value $a is equal to 2"
  73.         }
  74.         else
  75.         {
  76.             Write-Host "The value $a is less than 2 or is not created 
  77.         or initialized."
  78.         }
  79.  
  80.  
  81. SEE ALSO
  82.     For information about using comparison operators, enter the following 
  83.     command at the PowerShell command prompt:
  84.      
  85.         help about_comparison_operators
  86.  
  87.     For information about the switch statement, enter the following command 
  88.     at the PowerShell command prompt:
  89.  
  90.         help about_switch
  91.