Microsoft
Software
Hardware
Network
Question : General Question: Looping
I am trying to understand looping. I wrote a simple loop:
L1: for (
L2: $i = 0
L3: $i -le 5
L4: $i++
L5: )
L6: {
L7: write-host "counting to five" $i
L8: }
I put this loop into the Powershell ISE, and put break points on lines 2, 3, 4, and 7. When I execute the script, the debugger shows the following:
PS C:\WINDOWS\system32\window
spowershel
l\v1.0> X:\scripts\test\loop.ps1
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
2'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
3'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
7'
counting to five 0
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
4'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
3'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
7'
counting to five 1
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
4'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
3'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
7'
counting to five 2
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
4'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
3'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
7'
counting to five 3
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
4'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
3'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
7'
counting to five 4
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
4'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
3'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
7'
counting to five 5
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
4'
[DBG]>>> Hit Line breakpoint on 'X:\scripts\test\loop.ps1:
3'
My question are these - the loop only executes the command on line 2 the first time through the loop. Why doesn't it execute that line every time through the loop? After the first pass, the loop then executes line 4. Why didn't it execute line 4 the first time through the loop (first time through, it skipped from line 3 to line 7). Finally, why does it execute each subsequent loop in the order of line 4, line 3, and then line 7?
I can create loops, but I would like to understand how the work in the event I need to troubleshoot them. Can someone maybe provide a link to a good, concise explanation of looping?
Thanks, and Peace.
Answer : General Question: Looping
The definition of a "for" loop for powershell, and many languages, determines which part (or "lines") are executed at what time.
http://www.powershellpro.c
om/powersh
ell-tutori
al-
introdu
ction/logi
c-using-lo
ops/
http://en.wikipedia.org/wi
ki/For_loo
p#Three-
ex
pression_f
or_loops
Random Solutions
Resize multi select boxes in SharePoint form
How to make IN operator to accept CSV which are varchar in SQL Server 2008
Is there a funtion that if I use substr(col,1,1) will tell me where a value is not a character
Running RMI on Tomcat Servlet - How do I set up RMISecurityManager?
how long will deleted table hold values
Iphone Can't get Exchange mail on LAN but ok on 3G.
Code was working in Android 2.1 SDK but not on 2.2
Error Code 10061: Connection refused after replacing certificates
how to find the nodes inteconnectivity is propperly configured
What does it mean if I am receiving output errors on all of my Cisco Aironet 1130AG?