For and While Loops in C # Code! Beginner Lesson

LeMaster Tech
183 followers
2 Video Views·Apr 21, 2025
This tutorial takes you through everything you need to know to use For and While loops in your C# Code while programming with Microsoft Visual Studio!
C# (C-Sharp) is an extremely versatile programming language and is used as the driving code in a large number of applications including Unity for game design!
This is the fourth C# tutorial on this channel and I will start mixing these in with the other software tutorials we do going forward!
If you did enjoy this content or found it useful be sure to leave a like and subscribe to channel for more great content like this, and be sure to check out the channels for hundreds of useful tutorials!
Thanks for watching and good luck with your code!
what's going on guys welcome back to the
channel and today's video is going to be
all about for loops and while loops in
c-sharp and visual studio
[Music]
so this is a continuation in the c-sharp
tutorial series we've been doing on the
channel and a super important concept in
programming is using loops looping
functions that'll iterate numerous times
so you don't have to run
the same piece of code a whole bunch of
times and write the code over and over
again you can just call your loop to do
something
numerous times and the two main types of
loop are for loop and while loop and
we're going to get into both of them
right here so the one i want to start by
talking about is actually going to be
the while loop and the main difference
between a for loop and a while loop is a
while loop and you can maybe guess this
from the name but a while loop runs
until a certain condition is met so it's
not preset how many times it's going to
run it's going to continue until the
condition is met so you say while some
variable is under a certain value or
while you know game over is false if
you're building a game whereas a for
loop you define how many times it's
going to run right up front you say four
like i in range zero to five so you're
saying okay i want it to execute exactly
five times
and that's a for loop so let's go ahead
and do an example of a while loop real
quick i'm going to start by
and i have this just generic project set
up when you open up a new c sharp dot
net console
app in visual studio this is pretty much
what you get and then i always throw a
write line and a read line in
so if you are super beginner and you
need to understand how we got to here i
recommend you check out maybe the first
c-sharp tutorial on the channel
otherwise we're going to get right into
it so i'm going to start by
defining an index variable so an index
variable is just basically what we're
going to use to keep track of where
we're at in the loop and for a for loop
and while loop as you'll see in this
video you're gonna want one of these so
we'll start by just saying it equal to
zero
and then to is instantiate or start a
while loop it's just it's just the word
while and then inside of parentheses
you
give your condition for what you want
this loop to run during so while and
we'll say index less than or equal to
five okay
and so what we just did is we created a
new i guess kind of bucket of code where
everything underneath this loop
everything inside of these curly
brackets
um is going to happen anytime our index
value is less than or equal to five and
so this is a super important concept for
while loops if you don't put any code
inside of the while loop that's going to
change the state of the index
and you don't have anywhere else in your
program that's going to change the state
of the index you end up with what's
called an infinite loop and that's a
pretty quick way to crash a processor if
you don't put any safeguards in there
it's an infinite loop can't be processed
so if you start up a program and there's
no way the index ever changes um
then then you're gonna have problems so
just make sure kind of the first thing
when you create a new while loop is you
figure out what's going to get you out
of the while loop and so what we'll do
is we'll say every time this while loop
runs we're just gonna add one to it and
that's the plus plus it's a little bit
faster than doing um index
plus
um one so
the plus plus operator i talked about
this a little bit in a previous video
but that's basically saying the same as
index equals index plus one okay so this
is the same exact code as just writing
index plus plus but this is a little bit
faster way of doing it and it's great
for while loops just incrementing at
once so you can see we're gonna start
with index of zero right here and then
every time the index runs we're gonna
add one to it so as soon as it gets to
six we'll be out of it and all i'll do
then
inside of here is i'm going to take my
console.writeline
i'm going to move that in here and i'm
just going to write the index so we
should be able to see what this is doing
now if we go ahead and run it
and we should just get 0 1 2 3 4 5 on
the console so let's go ahead and run it
and see if i made any mistakes
there you go and hopefully you can see
we got zero one two three four five um
and then the index incremented up to six
and so we exited so the while loop is
great
uh if you want it to run while the index
is less than or equal to 12 or whatever
you you can just change it there but the
main piece of uh of take away from here
is a while loop will run forever until
the index
the index or
variable is met and so now let's go
ahead and change this to a for loop and
we'll talk about how that's kind of
different a for loop is always going to
have a fixed number of times that it's
going to run
so we'll just kind of modify around this
so we can keep using some of what we've
already got uh but to create a for loop
it's just four instead of while
and then one thing that's pretty cool in
a for loop you can actually
initialize your
index variable inside of this for loop
parentheses and you want to make sure
you keep that semicolon um that's still
important you want to keep that in there
but we'll say okay so
integer index is equal to zero and then
the second thing you give it
is the is the conditions just the same
so it for index equals to zero and then
index is less than or equal to five so
that's how many times we want to run it
and then you also give it what you want
to happen to the index every time it
runs
um so this is saying okay initialize the
variable
we want it to run until it's uh
it's greater than five essentially so we
want to run while indexes less than or
equal to five and then we want um to add
one to the index every time we go
through it and so let me just run this
and actually
so one thing i didn't remove but you see
we got zero two four it looks like it's
skipping a number that's because i left
this index plus plus inside the loop you
don't need this anymore when you create
a for loop and you tell it what to do
every time the index finishes
you don't wanna add a plus plus in there
that's going to make it skip over so
there you can see we changed it and we
get zero one two three four five um so
that's a super basic crash course on
four and while loops if that's all you
needed then um you probably have what
you need for this video i'll go ahead
and show how this can be applied in like
creating a power function um and so like
raising a number to an exponential using
a loop and i'll give you an example of
how that could work uh for
a for loop and for a while loop so since
we have the for loop set up here let's
just go ahead and start there
um and what we'll say is uh we for so
for this one we're going to have to call
a few variables before we get started
because all we can initialize in the for
loop instruction is the
index value so we'll say whatever number
it is that we want to raise it to and
we'll we'll say right now we'll use the
number four
and then whatever the power is that
we're going to raise it to so we'll say
we want to do four to the third power
um
and then let's make a variable that
we're going to store the answer in so
initially
we'll set the answer variable equal to
one
because if the power function was like
raise it to the zero power you'll see
when we get into writing the loop but we
would want it to just return a one so
starting it at one
is what we'll want to do and then we'll
have the indexer start at one
and we will say uh so as long as the
indexer is less than or equal to our
power variable then we want to do what's
what we're going to put in here and
let's go ahead and
clean this up a little and what we want
to do every loop then so this is saying
okay so far the index hasn't multiplied
the number
up to the power function yet
well we want to take that
answer and we want to multiply it
times the number
uh
and then store it um in the same
variable the same answer variable and
this times equals similarly to that plus
plus operator is the same as saying
answer is going to be equal to number
times answer okay so these these are the
same line of code uh if you were to say
the first time this ran your answer is
equal to one so we're going to come in
and say okay now the answer is going to
be 1 times 4 and that would be if we had
put in a power value of one but then the
next time it runs answer's already a
four so it's going to come in and say
answer is now equal to four times four
so it'll be 16. it'll check if we've
done the total number for the power yet
um and if not then it'll do it again so
just a shortcut of writing that is
answer and then times
equals number so it's basically saying
multiply yourself times the number and
store yourself in the same variable
okay and then we don't have to do this
index plus plus because we're in a for
loop just like we talked about and then
we will print
the answer out so uh we'll do it every
loop even though what you'd really want
from a power function is just at the
very end so you could move it out here
we'll do it every loop so you can see
the function
running
okay and you can see there we get 4 16
64 consecutively so we get the power
and if i were to change this to four to
the fifth power then that should be all
we have to do to get it yeah to go up
four to the fifth so this is how you'd
build like a super useful power function
now we did talk about the math methods
that are built into c-sharp in the
previous video so there is a nice pow
function already but understanding how
you get it is super useful too so let's
modify this um so that we can do it in a
while loop instead and then we'll end
this video
so
for the while loop like we talked about
you need to have your index variable out
here this needs to be
while
and then we need to
put this index plus plus condition ctrl
x
we want to put this index plus plus
right in here because it's not built
into the while anymore and let's see if
that's all we had to do i don't
all right there we go so uh yeah i
wanted to make this kind of quick i
didn't want to keep you guys here all
day so i just didn't check through it
real thoroughly but there you can see
it
for loops and while loops are somewhat
interchangeable there are definitely
situations where one is going to make
way more sense than another if you're
creating a game and you have like a game
over condition you would want to use a
while loop and say okay do all this
active code anytime the game is not game
over but in a lot of mathematical
situations it can come down to what you
prefer to write how you prefer to write
it um but understanding the two
situations it'll usually feel pretty
obvious which loop format to use um i i
like using for loops to generate arrays
for like board sizes like if you're
going to create an array for tic-tac-toe
or sudoku or anything you know exactly
how large and how wide it needs to be i
usually use a for loop just because it's
a fixed size but you could do it with a
while loop too and that's what i've been
trying to show in this video
how to understand for loops and while
loops how to use them both and how
they're interchangeable so if you have
any questions about anything you saw in
this video feel free to let me know
about in the comments below
otherwise thanks for checking out the
channel be sure to leave a like if you
found it useful consider subscribing to
the channel check out tons of other
great tutorials there and thanks for the
support and good luck with your code and
thanks for watching thanks bye
