Basics of Programming #10 – Iteration


Wow, we’re really making progress – we’ve
done “Selection”, and there’s only one more programming construct before we’re
done with the absolute basics, that doesn’t mean the series is over – no, it definitely
isn’t, but this is the final absolute basics video, after this we’ll be learning stuff
that helps you build more robust programs, I’ll talk more about that in Episode 12.
Let’s do the intro! OK, so, this video is about iteration now,
let me explain what iteration is by actually showing you first. So, let’s say we want to make a program
that can count from “1” to “10”. Now, what you would do, with the knowledge you
have is make a “WriteLine” that writes “1” onto the screen. Then, 2, 3, 4, 5,
6, 7, 8, 9 and 10. And, of course, this will work – but this is just horrible, we’ve
got ten lines of code. Or, let’s say we need to get the program
to input 10 users, you could make 10 variables – so, that’s 10 lines, then make 10 ReadLines
– that’s now 20 lines. And, let’s say now, that for every user I want it to do something
different, I would have to change all ten of these ReadLines – you just can’t build
a program like that – code duplication is really bad, you have no idea how painful it
is for me to look at this. This is really bad, your project will become a mess like
this, and in the end, you’ll spend more time changing all 10 of these, then you will
actually adding anything to the project. What you should do, is use iteration! So,
iteration allows you to repeat specific code multiple times, without actually writing it
multiple times. So, instead of having ten lines to count up to 10, I could actually
do it in two lines. And, if I wanted it to count up to 20 – I could just change one
number, and it will count up to 20. This is an example of iteration, but, obviously, we’ll
break this down in a minute. OK, so, there are two things we can use to
perform iteration, we can use a “for” loop, this lets you essentially count up from
one value to another. So, with a “for” loop, you could count up from 1 to 10. We
can also use a “while” loop, which will run until a specific condition is met, so,
kind of like an “if” statement, except it just loops around and around while a condition
is true. Let’s start with a “for” loop! So, the
actual declaration of the “for” loop is split down into three parts, now, it might
look like a lot at first, but as you get used to it, it won’t be so much, I promise. Each of these parts go in the brackets, and
they’re separated by a semicolon, so, like this. The first part is the variable declaration.
You see, in order to count from “1” to “10”, we need to actually put what number
we’re currently on into a variable. So, that variable is the thing that counts from
1 to 10. So, this first part is actually declaring, or making that variable, so, here it is, now,
we set it instantly here as well – we instantly set it to “0”, so it starts at “0”.
Now, there are two things to point out about this. Firstly, it is common practise to call it
“I”, now, normally you should be giving your variables very meaningful names – you
shouldn’t just be calling a variable one letter, it should have a name that makes it
clear what it is. However, a for loop is essentially the one place where it’s alright, because
all programmers are used to seeing the counter variable being “I”, it’s just a common
that you do. And, in addition to that, you’ll notice
that we’re starting at “0”, as opposed to “1”, we’re doing that because in
C#, and, once again a lot of languages, you tend to start at “0”. And we do that because
back when computers weren’t so powerful and some numbers were only a byte, we wanted
to take advantage of as many of the 256 combinations as possible, right? And “0” is one of
those 256 combinations, so by starting at “1”, we would have been limiting our variable
from having 256 different values, to having 255 different values, for literally no reason
at all. So, it’s just tradition really to start at “0”. When we talk about arrays
later on in the series, then starting at “0” will become especially important. Next, we need to set the condition, and, basically,
while this condition is true, the “for” loop will run. So, I could say, for example,
“I

Leave a Reply

Your email address will not be published. Required fields are marked *