C Programming Tutorial For Beginners

Limit is gonna tell us how many times they can guess and out of guesses is going to tell us whether or not the user is out of guesses so.

Down here we’re checking two conditions for this while loop we’re checking to see if the guess is not equal to the secret number and we’re gonna keep looping as long as out of guesses is equal to zero every time we.

Go through this loop we check to see if the guest count is less than the guessed limit if it is then we ask them to guess we.

Increment the guest count otherwise we say out of guesses is equal to one and that’s gonna break us out of the loop down here there’s two situations the first.

Situation is that they ran out of guesses the second situation is that they won the.

Game and we’re using this if statement to check both of those so that’s basically how this works and this kind of shows you how we can use things like while loops and if statements and also variables together in like one single program.

In this tutorial I’m going to teach you guys how to use for loops in C now a for loop is a special type of loop that we can use in C which allows us to use something called an indexing variable and this indexing variable will basically tell us.

What iteration of the loop we’re currently on and we can use that indexing variable to do a bunch of stuff like we could loop through an array of items we could do all sorts of stuff inside of a loop so down here in my program you’ll see that I have a while loop set up and I have just sort of a basic while.

I want to show you guys what it’s doing so essentially up here I’m saying int I is equal to 1 so I’m giving this integer I the value of 1 and I’m saying wow I is less than or equal to 5 I’m gonna print out the value of I and then I’m gonna increment I so this is a very simple wire loop basically we have this variable I and every time we go through the loop we’re printing it.

Out until it’s greater than 5 so let’s run this program and we’ll see what it does so you’ll see over here we’re basically printing out values between 1 and 5 and I want to point out what’s actually happening so the first time that we go through this while loop we’re printing out 1 the second time we’re printing out to the third time we’re printing out 3 in other words this variable I is basically telling us what iteration of the loop are currently on so on the first.

Iteration of the loop I is telling us that we’re on the first iteration of the loop right the first time we go through the loop I is equal to 1 second time we go through the loop I.

Is equal to 2 third time I is equal to 3 so this variable I over here is basically telling us how many times we’ve gone through the loop so on the third time it’s telling us 3 fourth time for etc and believe it or not this is actually a very useful thing for us to have when we’re looping it’s there’s a lot of situations where you’re gonna want to know what iteration of the loop you’re currently on when you’re looping so with a normal while loop.

Like I can basically just specify a condition.