C Programming Tutorial For Beginners

Gonna print out an integer and I’m just gonna print out index so we’ll be printing out index and actually why don’t we print out a new line every time to there we go and finally after this I want to increment the index variable so I can just say in X is equal to index plus one basically I’m adding one onto the index every time and actually I want to show you guys something in C there’s gonna be a lot of situations where you’re gonna want to.

Do something like this basically add one to an existing variable and in C there’s actually a shortcut we can use so I can.

Come over here and I could just say index plus plus and this is gonna do the same thing so this is just gonna add one.

To the index essentially what I’m doing is I’m saying I want to print out index and then I want to increment index and I’m gonna keep looping through this code while index is less than or equal to five so let’s go ahead and run this program and.

Maybe you can predict what’s.

Going to happen you’ll see over here I’m printing out the numbers one through five so I’m printing out one two three four and five and let me walk you guys through.

Exactly what’s happening in this loop over.

Here we create this index variable and.

We set it equal to one and basically when C goes to execute this program the first thing it’s gonna do before it does anything else is it’s gonna look at this condition and it’s gonna check the condition and it’s gonna say okay is index less than or equal.

First time through index is equal to one so it’s definitely less than five C’s gonna go.

Through it’s gonna execute this line of code printing out one then it’s gonna execute this line of code incrementing index then what C’s gonna do is it’s gonna come all the way back up here and it’s gonna check this condition again so it’s gonna say okay is index less than or equal to five.

This point is equal to two so it’s less than five so it’s gonna go through execute this line of code execute this line of code now index is equal to three and C’s gonna go all the way back up to the top it’s gonna check this condition again so before C goes through this loop it’s always gonna it’s always gonna check the condition first so before it’s allowed to go and.

Execute that code again it has to check the condition to make sure that.

It’s true eventually we’re gonna get to a situation where index gets incremented and it gets incremented up to 6.

Then C’s gonna come back up here and it’s gonna check to see if 6 is less than or equal to 5 it’s gonna be false and we’re gonna move with our lives in the program and that’s the basics of while loops so this seems very simple and that’s because it is all we do is specify condition as long as that condition is true we keep doing all this stuff in here and there’s tons of.

Where while loops are gonna come in handy this is just one of.

Them where we can print out like numbers between 1 and 5 I also do want to point out one thing that you might want to watch out for which is called an infinite loop and an infinite loop is a situation.