C Programming Tutorial For Beginners

Up here and I can do whatever I want right it’s very open you can kind of just do whatever you want with it but in a lot of situations and there’s.

Tons of these situations we’re going to have a variable just like I that will tell us what iteration of the loop that we’re currently on or you know basically just a variable that’s gonna keep changing every time we.

Go through the loop and this is such a common situation in see that there’s actually something called a for loop and a for loop allows us.

To take all of this code and condense it into its own single loop so it’s taking this situation where we want to have a variable like I and it’s basically allowing us to do it a lot easier and a lot cleaner so I’m gonna show you guys how we can create a for loop I’m gonna come down here below this while loop and I’m.

Gonna create a basic for loop structure so I’m just gonna say for I’m gonna make an open and closed parenthesis and an open and closed curly bracket now the difference between a for loop in a while loop is basically gonna happen inside of these parentheses so in the parentheses of a while loop we.

Have our loop condition or our loop guard right this is specifying whether or not we can keep looping through inside this for loop we’re.

Gonna have three different things so instead of just one loop guard we’re actually gonna.

Have three different things that we want to put inside of here so with a for loop the first thing I’m gonna put in here is this variable I so.

Loop we have our variable I up here and this is basically allowing us to loop through and keep track of how many times we’ve gone through the loop so what I can do down here is I can do something similar I could say like int I and I’m not gonna give this a value inside of here I’m gonna say I is equal to 1 so basically I’m gonna take my variable I and I’m.

Gonna give it an initial value of 1 and now I have my I variable I have my variable that’s gonna change every time we go through the loop the next thing I want to do is include my looping condition so up here we have I as less than or equal.

To 5 that’s our condition I can put the same thing down here so I’m gonna.

Say I is less than or equal to 5 the third thing I want to do is increment I so you’ll notice down here every time we go through this.

Loop we’re incrementing that variable I I’m gonna do the same thing over here in this little third.

Quadrant so you’ll notice I have these little like sections here’s the first section we’re initializing the very i were saying i is equal to one here we’re specifying our looping conditions so i’m saying we’re gonna keep.

Looping while I is less than or equal to five and over here is a little line of code that’s gonna get executed every time we go through the loop so here I’m saying I plus.

I minus minus and that would decrement I I could say like I is equal to I plus two etc like I could do basically anything I wanted over here let’s just keep it at I plus plus for now so you’ll see I was basically able to take all of this code and condense it into its own little type of loop and now instead of having to like.