1.

Explain what is a "while loop" in PowerShell?

Answer»

IT professionals make use of LOOPS when they require to complete a block of commands numerous times.

Example: An entry-controlled loop, a "while loop" runs commands consecutively as long as the provided condition is true. A bunch of IT professionals prefers to EMPLOY "while loops" rather than "for STATEMENTS" as the syntax is less complex."

The following example prints the values from 1 to 5 using the while loop:

  1. PS C:\> while($count -le 5)
  2. >> {
  3. >> echo $count
  4. >> $count +=1
  5. >> }

Output:
1
2
3
4
5

In this instance, the condition ($count is less than equal to 5) is true while $count = 1, 2, 3, 4, 5. Every time through the loop, the value of a variable $count is incremented by 1 utilizing the (+=) arithmetic assignment operator. When $count EQUALS 6, the condition statement assesses to false, and the loop exits.



Discussion

No Comment Found