# C Programming Tutorial For Beginners

Common but just you.

About do-while loops because there are certain circumstances where they’ll come in handy basically any time you don’t want to be checking the condition first you can use a do-while loop but those.

Are the two basic types and there’s actually another type of loop that we’ll talk about later in the course which is called a for loop but for now we’re gonna have some fun with while loops and.

In the next tutorial we’re gonna be building a little guessing game in this tutorial I’m gonna teach you guys how to build a little guessing game and see this is gonna be pretty cool because we’re gonna use a lot of the stuff that we’ve learned up to this point in the course in order to build it so let’s get.

Started basically this guessing game is going to allow the user to guess a secret number so we’re gonna define a secret number and we’re gonna give the user the opportunity to try and guess it so if.

They’ll try to guess it if they get it right then they’ll win the game and if they don’t get it right then we’ll let them guess again right so we’ll basically let them keep guessing what the secret number is until they get it so down here we want to create a couple variables the first variable I want to create.

Is gonna be an integer and it’s gonna be the secret number.

So I’m just gonna call it secret number and why don’t we just set.

It equal to five the next variable we want to create is gonna store the user’s.

Guess so this is also gonna be an integer and I’m just gonna call it guess and I’m not gonna give it a value yet because right off the bat.

The user wouldn’t have guessed a number so what we want to do is we want to be able to prompt the user to enter in the number until they get it right so as long as the user hasn’t entered in the correct.

Number we want to keep asking them to enter it in in order to do that in order to like keep asking them to.

Guessed it and we can use something called a while loop so I’m just gonna type while and when making open and close parentheses and an open and closed curly brackets let’s think about a good condition for our game basically we want to keep asking the user to enter an input as long as they haven’t guessed the secret number so it’s.

Right there we could keep looping as long as the guess is not equal to the secret number right and basically this is always going to be true until they guess the correct number now inside of here we want to prompt them to enter their guests and then we want to take whatever they enter and store it inside of our guest variable so.

Let’s do this first we’ll say printf and I’m just gonna say enter a number and.

Then down here I’m gonna use scanf in order to get whatever number they.

Enter and I’m gonna store it inside of that guest variable so I’m just gonna say at guess and this is gonna take whatever number or whatever integer they enter and it’s gonna store it inside of our guest variable so basically what’s happening in this loop is every time.

Through the loop we’re getting a different number and then we’re gonna come up here and we’re gonna check to.

See if it’s equal to the secret number if it’s not then we’re gonna go through again and we’re gonna keep doing this but if the guest is equal to the secret number then we’ll break.

Out of the loop and that means we’ll.

Be down here so I’m just gonna print out a success message and I’ll basically just.

Say like hey you win all right awesome so you can see this isn’t actually that many lines of code I mean it’s probably like ten lines of.

Code to write out this whole thing and it looks like everything’s set up so this should actually work properly so let’s try to run this and we’ll see if we can guess the secret number so remember a secret number is.

Five so I’m gonna run the program it says enter a number so let’s just kind of botch the guesses two seven eight nine three so you’ll notice I can keep entering in numbers and it’ll keep prompting.

Me to enter a number as long as I haven’t guessed correctly.

So now let’s type in five and remember five is the secret number when I click enter now that loop condition is going to end up being false right because.

The guess is gonna be equal to the secret number.

And it’s gonna execute and it’s gonna say hey you win right so we broke out of that loop we.

Executed the print statement that said you win and the program terminated and that is a really cool little game so one of the.

Game and it’s different from programs that we’ve written in this course in the past is it’ll keep running essentially infinitely until we enter in that correct guessed so it’s gonna keep going and keep going keep asking us to do something and to enter in a value until we entered correctly.

So this is pretty cool now this is a pretty nice game and it’s it’s pretty.

Simple but I want to maybe make it a little more complex one of the things that sucks about.

Our game right now is that the user gets.

Unlimited guesses right so basically like they can just guess every single number and eventually they’re gonna get it right you know so why don’t we do this.

We should impose a guess limit so why don’t we say like the user can only guess three times and if they can’t get the secret number in.

Three tries then they’ll lose the game so let’s think about how we can go ahead and add that functionality into this little app I’m gonna walk you guys through how we can do that the first thing we’re gonna need to.

Do is we’re gonna need to create some variables I’m going to create another variable up here and I’m just gonna call it guess count and guess.

Count is basically going to tell us how many times the.

User has tried to guess the secret number and I’m just gonna set this equal to zero because initially the user will have guessed zero times so remember this guest count variable is basically gonna get incremented every time they.

Guess right so I’m gonna come.

Down here every time we go through this loop the user is guessing so every time we go through this loop I’m gonna increment the guest count so I’m just gonna say guest count plus plus and that means every time the.