Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm making a syllabus to teach non-programmer co-workers PowerShell. The first non-pilot group I will run the course with will be a team that deploys and configures one of our software products. They are bright intelligent motivated IT workers, but not programmers.

I intend to cover the basics (variables, arrays, functions, and loops), and then move onto examples that are close to their problem domain. I'm not married to a 4 hour class, if necessary I can expand it to two four hour classes.

I'm toying with the idea of skipping for and while loops and simply teaching ForEach-Object. I've personally used traditional for and while loops in powershell scripts, but they can always be rewritten as ForEach-Object loops with a bit of creativity. Part of me feels the good old for ($i=0 $i < foo; $i++) is a necessary fundamental before moving onto foreach, but the other part of me thinks that might be me clinging to my Model M and my K&R Bible.

To draw an analogy to SQL, I'd never teach a CURSORs or other loops in a 4 hour class there because you're supposed to be able to do everything in SQL as a SET operation. In theory you should stick to the pipeline in PowerShell. I use for and while loops in PowerShell more often than I do do cursors and while loops in SQL. However, PowerShell is a scripting language and conventions are less strictly enforced and I also push the boundaries of powershell with inline C# and excessive dependencies on calling native .NET APIs. My audience will not be ding this.

For those not adept at powershell, I'm pretty sure you don't actually need a for or while loop. I use them, but that's because I have experience with other languages. For example if you only want the first 10 members of an array of 20 members:

1..20 | Select-Object -First 10 | ForEach-Object { Write-Host $_ }
share|improve this question
add comment

6 Answers

Why skip them? I appreciate you are time restricted, but include the syntax of each with a little example in the course notes at the very least. You don't have to spend more than 30 seconds describing them.

If they can't code a "display the first 10" easily I think you've missed a trick.

share|improve this answer
    
I just added an example of how to do that easily without a for loop. The thing is it takes 30 seconds to describe a loop to some people, but loops are one of those things that some non-programmers just don't grok. I'll have to see in my pilot if explaining the for and while syntax really takes 30 seconds, –  Justin Dearing Sep 8 '11 at 10:56
add comment

Touching on the basic loop constructs is very important -- if for nothing else than giving the students a fighting chance when they see the for/while constructs in the wild.

share|improve this answer
add comment

I think the key part of your post rather spells out that you shouldn't skip them:

I've personally used traditional for and while loops in powershell scripts, but they can always be rewritten as ForEach-Object loops with a bit of creativity.

I feel that it's good to know how to do the basic for/while/do loops so that you can take the next step to realize that creativity. It really doesn't take long to teach loops to people, and it would be a good lead in for the foreach loop.

So why not make it for/while/do/foreach, and walk though each one? You can even say right in the training that the foreach-object loop will be the one they use most, and you could spend more time on it.

share|improve this answer
add comment

I think this hinges on what kind of programming experience they have coming in --

If they are rank beginners and have never seen loops/control structures before they definitely need to see traditional for and while structure: They'll see it in the wild (in nearly every language they'll ever use), and it shouldn't be a surprise when they do.

ForEach-Object, much like Perl's foreach is a tasty shortcut dipped in syntactic sugar. They should know it because it saves a lot of typing and is logical when you read it.
If they're already programmers (or have at least some limited experience) they'll grasp the concept quickly. If not, contrive a for loop that is easier to do with ForEach-Object, and have them do it both ways (for loop first, ForEach-Object second) so they see a real and immediate benefit.

share|improve this answer
    
I'm going to have to write the sylabus out and pilot it before I make a decision. I added one final apology to my question, a SQL analogy, but I don't think it changes your answer here. The arguments agains for/while are time and the pipeline is good enough in powershell. The arguments against are what you laid out. –  Justin Dearing Sep 8 '11 at 17:14
1  
That's definitely a good point - PowerShell is supposed to encourage you to use the pipeline syntax for everything (and it's definitely "good enough" if not "better than"). You could always include traditional control structures on a one-page "More stuff you should know" type handout too. –  voretaq7 Sep 8 '11 at 21:46
add comment

I find myself using the for and while loops a lot in powershell. When I have a boolean I just write while($bool). I think that it is important to atleast touch on these concepts, considering they are found in a lot of other programming languages.

A better example that you may not be able to do with a foreach loop (effeciently atleast) is take every other object out of an array:

for($i=0; $i -le $arr.length-1; $i=$i+2) {$newArr += $arr[$i]}

I find myself using this one alot after a -split

share|improve this answer
add comment

Traditional for and while loops I don't think add much value to a 4 hour course. Ideally they are taking your course so they can write scripts and get stuff done. A ForEach loop is easier and sufficient to get the job done most of the time. If they need the traditional loop construct then they can always learn it when they need it. Same thing with objects, they don't need a deep understanding, just enough to know how to use methods.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.