C Programming Tutorial For Beginners

Where this condition up here never becomes false so this condition will just always stay true and there are certain circumstances where you’re gonna want to use something like an infinite loop but in a lot of circumstances.

You’re not gonna want to go anywhere near something like that so let me just demonstrate so you guys can see so if I stopped incrementing this if I just got rid of index plus plus now when I run.

My program you’ll see that it’s just continually printing out ones I mean look at all these ones that are getting printed out you can see like the text editor.

Is kind of going crazy this program is just it keeps executing through that loop and it’s still going right now so this would keep going forever if I let it keep going infinitely hence.

The name so I’m just gonna terminate this but that’ll kind of show you like how an infinite loop works and you definitely want to watch out for that because you’ll get into situations as.

You work with while loops where they’ll just become infinite.

And you you know you need to watch out for that especially if you’re doing something a little bit more advanced where you’re like siphoning off memory or you’re creating processes or something it can slow your computer down quite a bit so this is a while loop and this.

Is a really great loop there’s also one other type of loop that’s similar to a while loop that I want to talk to you guys about and before I do that I just want to do something really quick so I’m gonna set this index equal to 6 all right I’m gonna set this equal to 6 and I’m gonna run my program and what you’re gonna.

Notice is nothing’s gonna get printed out so the code inside of this while loop isn’t gonna print out because the first thing we always do when we were on this while loop is we check this condition so the first thing C’s gonna do before it prints anything before it increments anything it’s gonna check this condition and if it’s false it’s not gonna do any of this stuff so when.

I run this program you’ll see nothing’s gonna get printed out and that’s exactly what happens there’s another type of a loop that’s similar to a while loop which is called a do-while loop and I’m going to show you how to make one all you have to do is take this.

Wild thing up here and paste it down here below and then up here I’m just gonna say.

Do and a do-while loop is similar to a while loop but the first thing a do-while loop does instead of checking this condition down here is it.

Executes all of this code so even though index is equal to 6 we’re still gonna print out 6 and we’re still going to.

Increment it and then we’re gonna check the condition to see if we can keep looping so let me show you guys this and this should illustrate what this is doing and actually I need to put a semicolon here so you can see over here.

We’re printing out 6 so unlike before when we just use that while loop now because we’re using a do-while loop we’re doing whatever was inside of that loop before.

We’re checking the condition and there’s gonna be certain circumstances where a do-while loop will come in handy I would say by far while.