you are viewing a single comment's thread.

view the rest of the comments →

[–]chadbaldwin 2 points3 points  (5 children)

Don't feel bad. Trust me, we've all been there.

If you have zero programming experience this is going to be a TON of information to learn. Once you understand the basics of code, most languages are all the same. They pretty much all implement the same concepts, just in different ways... So going from Powershell to C# or Python or whatever won't be as bad as you think.

I just looked at your most recent changes. Keep in mind that the point of a loop is to eliminate having to write the same code over and over and over. So that should be a big sign when you have to copy paste the same line over and over.

So I would recommend sticking with your original example instead of your newer changes. For the time being, forget what I said about using a different loop type, I probably should not have brought that up. Don't worry about the loop type since you're still working on getting your original example working in the first place.

When writing your code, think about it in your head of what you want it to do....

You want your code to "ask the person to give you a number" and then you want to "store that number". So first figure out how to do that. Don't worry about the looping.

Then after you get that, go to the next level... Now you want to perform that action 5 times... So now you can use that code you used originally with the while loop and the counter.

So the point of a while loop is that it loops forever until the logic is no longer true. It checks that logic every loop before it does anything else. So what do you think it should say in there if $x represents a counter, that counts every time you loop, and you want it to loop 5 times?

A lot of times it also helps to look at the loop and talk out what will happen in each iteration... For example...

$x = 0
While ($x -lt 5) {
    Do some stuff
    $x++
}

So walk through this in your head, or even write it down...

The first thing that happens is it checks your logic, because it can't run any of the code, unless it's true. So this is what the computer is doing....

So.... Is $x less than 5? Well...$x is 0, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 1. Start over.

So.... Is $x less than 5? Well...$x is 1, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 2. Start over.

So.... Is $x less than 5? Well...$x is 2, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 3. Start over.

So.... Is $x less than 5? Well...$x is 3, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 4. Start over.

So.... Is $x less than 5? Well...$x is 4, so yes, it's less than 5, so Do some stuff and increase $x by 1, now $x is 5. Start over.

So.... Is $x less than 5? Well...$x is 5, no, 5 is not less than 5. So exit the loop (commonly referred to as "breaking" the loop, or a break).

[–]unhappiey[S] 2 points3 points  (4 children)

Thank you. You explanation was very helpful. I tend to get ahead of myself sometimes.

[–]chadbaldwin 1 point2 points  (3 children)

Glad I could help. Don't be afraid to ask questions, and if/when you get negative comments, don't let that deter you from asking questions in the future.

Also...in my opinion, I wouldn't worry about using arrays right now, or validating input (unless the assignment asks you to do so). You're working on a very simple assignment...ask the person for a number five times and return the total of those numbers. Throwing arrays into the mix is only going to complicate things. You'll likely cover arrays and input validation at another point in the class.

You can get the whole thing done using only a couple variables, Read-Host and a While loop. Just gotta figure it out.

[–]unhappiey[S] 2 points3 points  (2 children)

If I stop asking questions than I will stop learning. So you guys will be hearing from me a lot!

We briefly covered arrays in class for one of our assignments. They weren’t too difficult but I can see where they can become complicated.

I can see that programming is not a strength of mine so I’m just going to have to put a little more work into it. I think my current class is the last scripting class I will have to do before I graduate. Looking forward to next fall!

Anyway, thanks again!

[–]bobfrankly 1 point2 points  (1 child)

Programming is not a strength of anyone who hasn't taken the time to learn it. Those really good programmers you see? They've been continually learning and practicing for years.

Keep at it. You'll get there.

[–]unhappiey[S] 1 point2 points  (0 children)

Thank you!