C Programming Tutorial For Beginners

Printing out an integer and over here I’m gonna call this function so I’m just gonna say Max and I’m gonna pass in two numbers so we’re passing a 4 it will pass in a 10 so I’m passing in a 4.

And I’m passing in a 10 and this is going to return to us whichever one is bigger in other words this should return 10 so let’s go ahead and run our program and you’ll see over here we’re returning 10 so we got the value of 10 back from this function let’s try it with the other number so let’s make the.

First number bigger so now we have 40 and 10 so this should give us 40 back awesome so.

Over here we have an awesome.

Function right we’re basically checking to see if num 1 is greater than num – if it is then we’re gonna return num 1 otherwise we’re going to num2 and it’s important to note that even if I made these equal so if I made it forty and forty we’re.

Still going to get back the correct answer so it’s still gonna give us forty.

Back so this is the basics of using if statements right we say if we specify a condition if that conditions true we’re gonna execute this code if that conditions false we’re gonna execute this code down here and that is an extremely powerful structure in our programs and we.

Can really use this to respond to different input right so depending on what the user puts in num1 and num2 we can do different things but this is just sort of the beginning so why don’t we try to.

Make this function a little bit more complex so down here we’re allowing the user to input two parameters so we’re accepting two parameters into this max function num1 and num2 and we’re able to spit out which one is bigger but let’s up the ante a little bit what if we wanted to accept three parameters so instead of just passing in two numbers we wanted to be.

Able to pass in three numbers and have the max function spit out the biggest so over here we can modify this max function so I’m just gonna make another parameter so I’m going to say int and we’ll call this one num three so.

Now this function is accepting three parameters num1 num2 and num three but in order to figure out which one of these is the biggest we’re actually.

Need a more complex if statement so I’m gonna get rid.

Of this and let’s just start from scratch so let’s think about.

Which one of these is the biggest well what I would recommend is we could say if num 1 is greater than num2 and num 1 is greater than num 3 then we know num ones the.

Biggest right so if num1 is bigger than num2 and it’s bigger than um 3 we know it’s the biggest if num2 is greater than num1 and num2 is greater than num 3 then we know num2 is the biggest and otherwise we can just say that num 3 is the biggest so I’m gonna show you guys how to do this.

And we’re actually going to be introducing a couple more concepts for if statement so I’m just gonna say if and I want to check a condition so I.

Basically want to see if num 1 is greater than or equal to num 2 and.

Num threes so down here I can say num one is greater than or equal to num2 and now i.

Also want to check to see if num1 is greater than or equal to number three because if num1 is greater than or equal to num2 and it’s greater than or equal to number three we know num1 is the biggest so I can use this.

Special signal here this is called a logical operator and basically this is going.

To allow us to put another condition in here so I could say num 1 is greater than or.

Equal to num 3 basically what this and does is it allows us to put two conditions so we can check two things and this whole thing is only going to be true if this conditions true and this conditions sure so if one of these guys is false then this if statement.

Isn’t going to get executed so down here I’m just gonna open this up and if this is the case that we want to set result equal to num 1 because if num 1 is greater than or equal to num – and num 1 is also greater than or equal to number 3 then we know num one’s the biggest here’s the thing we also want to check to see if num 2 is.

The biggest so if this is false we also want to check to.

See if num 2 is the biggest now we can use something called else if so down here I could type out else if.

And I can make another open and closed parenthesis and inside of these parentheses we can actually check.

Another condition so if this stuff up here at this conditions false then else if is saying let’s come down here let’s check another condition so here I want to do basically the.

Same thing I’m gonna check to see if num.

2 is greater than or equal to num 1 and num.

2 is greater than or equal to num 3 so if this condition is true if num 2 is greater than.

Or equal to num 1 and it’s greater than equal to num 3 then we know for a.

Fact that result is gonna be num – because that’s the biggest now finally down here we can just say else and we can make another to open and close curly bracket and remember this code is going to get executed when neither of these conditions is true so here we can just set result equal to num 3 so let me walk you guys through this one more time up here we have.

This if statement and in addition to checking to see if num1 is greater than or equal to num2 we’re also using this special operator called and and we’re checking to see if num1 is greater than or equal to num 3 if that’s the case then result is going to be equal to num 1 otherwise if this condition is false then we’re gonna come down here and.

We’re going to check this other condition so we’re gonna check to see if num 2 is bigger than num 1 and if it’s bigger than num 3 if that’s true then we’re gonna do this finally if neither of these conditions up here is.

True we’re just gonna go ahead and set result equal to num 3 and then we’re gonna return.

Results so this looks good to me let’s test this out and we’re gonna see if it works so now when we call max we need to include three numbers so why don’t we just do 1 2 & 3 and this is basically going to spit out the biggest one which.

Should be 3 so let’s run our program and you can see over here we get 3 so let’s try to make one of the other ones the biggest so we’ll make the 1 in the first slot the biggest and let’s.

This again and you can see that it returns 10 so this function is working so that’s how we can.