13 Video Views·Apr 21, 2025
In this video we cover the basics of using for loops and while loops in C++ programming using the Microsoft visual studio IDE!
For loops run a function of code a set number of times based on the 'for' condition, while a while loop can run endlessly while waiting to see a certain ending trigger condition!
These two types of loop are very similar and can be used interchangeably in a lot of scenarios but each have specific benefits and advantages and knowing how to write both will always be best for the developer!
if you have any questions or want to see something specific in a future video let me know about it in the comments below! If you found the content useful, leave a like, subscribe to the channel and be sure to check out the channel for a ton of great programmer tutorials!
Thanks for watching and goodluck with your code!!
what's up you guys welcome back to the
channel and today's video is going to be
a continuation in our c plus tutorial
series in this video i'm going to show
you a basic introduction to four and
while loops in c plus using microsoft
visual studio
[Music]
all right so let's jump right into it a
for loop and a while loop by far in
programming the two most common types of
loops essentially a while loop is going
to run indefinitely until a certain
condition is met a for loop is going to
run at a predetermined number of times
and these are two different ways to
create recurring code that you want to
have occur a certain number of times a
lot of times while loops are used in
like games waiting for certain events to
trigger actions and a for loop is
something like populating an array or a
list where you know the overall size of
it so both of them are valid in most
applications of programming a lot of
people just end up with one that they
prefer more than the other and in c plus
it's just like any other programming
language
it's no exception almost any time you
use a while loop there's a way of
setting it up with a for loop but they
definitely have advantages and a few um
little differences between each other so
let's just get right into um writing
examples of both
okay so let's start with the while loop
and getting started here i'm going to
start by creating a variable that i'll
call in in index
and i'm going to just call it 1
initially so
in indexes if this is
if you're familiar with programming
concepts this might be something you've
heard before it's essentially where
you're at in a list or a sequence or in
a book it quite literally tells you like
uh it's a list of different events um
like what page you can find different
concepts on so it's a good
thing to know index is typically where
you are in a list uh how far you've gone
in a for loop anything like that that's
what an index variable typically is
and then i'm gonna print out just on the
screen
that we're doing this stuff for the
while loop because as you'll see
the while loop is going to run a whole
bunch of times and then after that we're
going to put the for loop to kind of
show both of them working so we're just
going to put on the terminal window that
this is the while loop we're working on
now to actually start a while loop and
call it you put while like this and then
in parentheses
you just put your condition for what you
want it to run until and for this we're
just going to say while index is less
than or equal to 10. it's a really
simple concept a really easy while loop
to think about
and basically the first thing you need
to do when you create a while loop is
add the condition that could exit you
from that while loop programming
it's not valid to have an infinite loop
which is a while loop that you can never
escape so basically first thing you want
to think about is how do i escape this
loop because an infinite loop will crash
your computer or worse in old hardware
that's the kind of thing that could
create like a fuse to blow so
i'm going to use the c plus shortcut for
adding one to a value which is a double
plus and so inside this while loop i'm
just going to say add one to the index
value now this while loop if i were to
run just this program
it would run but what you'd have is
really useless because we're not
displaying the result anywhere but
what's going to happen is this code's
going to boot up it's going to see that
index is 1 which is less than or equal
to 10 so then it enters this while loop
and it's going to say okay index is 1
and then add 1 to it and go to 2 and
then it's going to see while index is
less than or equal to 10 well 2 is still
less than equal to 10 so it'll run again
3 4 5 6 7 8 9 in that loop and then 10
again
so to show what's going on here let's
actually print the index that we're
currently on onto the screen and so the
code is going to run so fast that if we
weren't using that index for any real
thing you'd never know this had run 10
times but if i print the index onto the
terminal window if i run this now
you're gonna see on the window here you
go while loop one two three four five
six seven eight nine ten so that code
ran 10 times it did it so fast that we
couldn't process that code was happening
ten times
but our loop executed 10 times so these
are really useful when you don't
necessarily know the event that's going
to exit because right here you're saying
well we told it to run exactly 10 times
why wouldn't you use a for loop for that
and you're right you don't
need this to be a while loop we can make
the exact same loop in a for loop and we
will in just a second um but i wanted to
show the basic format of how you set up
a while loop a lot of the times you'd
say
while grade is less than 90 or something
and you're just looking for a student
who has an a in your class so you'd say
while grade is less than 90
index go through
the this index of my class and look at
the different students and their grades
and if they are less than 90 then just
keep going so i want to keep looking
until i find a student with an a
something like that
now let's go ahead and i'm going to copy
these uh this kind of overall
just so i can change it to for loop now
and let's take a look at what we do
instead of a
while loop to set up a for loop okay and
for this it's still you know you start
with the word four instead of while
that's probably the most obvious part of
the formatting but then this next part
is you give it three arguments you give
it uh first thing you want to give it
the indexer value index or variable
and for this we're going to create an
index that's just called i instead of
index and i'm going to set it equal to
an initial value of 1.
then we say the conditions we want to
run the for loop on so we'll say i is
less than or equal to 10 okay and this
is always going to be essentially what
you would put inside the while loop
condition this is saying how many times
to run it so while i is less than or
equal to 10
we are going to have
the i index or variable operate in this
for loop and then the third argument you
give a for loop is what to do to your
index or variable um and it's going to
be in this case add one to it every time
so if you want the index to like um
multiply by two every time instead you
could do uh like times equals two so
basically this would go one two four
eight sixteen there's all sorts of
different things you can do with this
but basically we're just going to add
one to do the exact same thing and then
instead of
this index
plus plus down here
we can just get rid of that line because
now we have the uh index plus plus in
the command loop for the for loop and
then we will just c out console out the
uh result of i now and so when i load
this up you'll still see that one
through ten for the
while loop but you'll what you'll also
see
oop
looks like i paused it
there we go what you'll also see
is 1 through 10 for the for loop right
underneath it so you have while loop one
two three four five six seven eight nine
ten for loop one two three four five six
seven eight nine ten okay so obviously
what they're outputting right now is
exactly the same because i was showing
you that you can use these two loops for
a lot of similar concepts it's just up
to you the designer the programmer to
determine which way is going to be the
right way for your application
but let's take a look at let's say it's
not as generic as
just iterating through and displaying
the index let's say we had an array of
values so integer
array of values with these square
brackets and then we're going to put the
actual values in curly brackets
and let's just give it some values 1 2
45 63 23 7
6 5 8 9
78
105
okay so if we were to say now
for
the same loop that we just set up
instead of just showing me
i go ahead and show me
the
index
values
at i so um if you're not familiar with
this indexer which i was talking about a
little bit earlier in the
in the program in the video
each value inside of this array inside
of this list has what's called an index
value that tells you where in the list
it is so i'm going to go ahead and run
this
and you can see instead of 1 2 3 4 5 6 7
8 9 10 we're getting all of these values
2 45 63 23 which if you look just below
the console down here 2 45 63 23 pretty
cool all the way through 78 the reason
it started at 2 and stopped at 78 is
because lists start they don't start
from one in terms of index everything in
programming across pretty much every
programming language starts from an
index of zero so this one is actually at
i equals zero which means this is one
two three four 5 6 7 8 9 and the 78 is
10. so if we wanted that 1 that first
character to show up we just say i's
initial value is equal to 0. now if i
run this we'll get the same stopping at
78 but now we're gonna start from one
which is great and if you wanted that
105 you would just change this to 11 and
then you'd get everything in the whole
list so
that's just a basic example of how to
use for loops how to use while loops in
c plus plus but hopefully uh you found
that informative if you have any
questions on what you saw in this video
or want to let me know what you want to
see next on the channel be sure to let
me know in the comments below um be sure
to leave a like on the video subscribe
to the channel all the support helps out
a ton and as always thanks for watching
and good luck with your projects thanks
bye