Up printing out true and you can see we just don’t print but if I put a false condition in here like if I said three is less than two this is false but because we’re negating it now it’s gonna end up being true so when I run my program you can see that we get true so that’s sort of the basics of using if statements and I cover just about everything that you can do we can use ands we can use all these different comparison operators we can use ORS and.
We can use this exclamation.
Point to negate something in this tutorial I’m going to teach you guys how to build a calculator in see have you been following along with this course you’ll know that in the.
Beginning of the course we actually created a calculator and it was very basic calculator basically we let the user input two numbers and we took those.
Numbers we added them together and we printed the answer out onto the screen in this tutorial I’m gonna take some of the stuff that we’ve learned since then and show you guys how we can build a fully functional for function calculator so this calculator will be.
Able to add subtract multiply and divide.
And we’re gonna let the user decide which one they want to do so we’ll let them decide if they want.
To add or subtract or whatever it’s gonna be pretty cool and we’re gonna end up using some of the stuff that we’ve learned.
Recently in the course like if statements we’re also gonna use like getting input from users and I’m gonna show you guys how you can get characters as input from a user as well so down.
Here we want to start making our calculator basically what I’m gonna do is I’m gonna ask the user to enter a number then I’m gonna ask them to enter an operator like plus minus division or subtraction finally we’re gonna ask them to enter in a third number and then we’ll depending on what operator they specify so addition and subtraction whatever we will perform the correct operation and we’ll print out the number so the first thing I want to do is create variables where we.
Can store the numbers and the operator so I’m gonna make some double variables and I’m just gonna call this one num 1 and then we’ll make another double call it num2 finally we’ll make a variable that will store the operator that they enter so this is just gonna be a.
Char and I’m just gonna call it op4 operator all right so now we want to actually get input from.
The user I want to figure out what numbers they want to use and then what operator they want.
To use as well so down here why don’t we get some input the first thing I’m going to do is.
Print out a prompt so I’m just gonna say enter a.
We want to do is we want to be able to take the number they give us and store it inside of one of those doubles.
That we created so I’m just gonna use this scanf function scanf and in here I’m.
Gonna specify that we want to get a double so remember when we’re using scanf if we want to scan for a double we use F and L F is going to scan for a double and now I’m going to use a comma and we’ll put the variable that we want to store.
In here so I’m gonna use this ampersand and I’m just gonna say num 1 so this is exactly what we need to do to be able to get the users input and store it into num 1 the next thing we want to do is get the operator so I’m gonna type out another prompt and I’m just gonna say enter operator basically they’ll be entering in plus sign minus sign asterisks or forward slash depending on what they want to do and again now we’re going to scan for a character now when we’re.
Scanning for a character and scanf and actually i don’t think i’ve talked about.
This yet in this course we want to do something special so I want to put a % and a C but before I put % C I want to put a space so whenever we’re getting a character from the user using scanf we always want to put a space right here before % c otherwise it’s not gonna work and again we want to store this in that.
Opie variable so I’m just gonna say o P and one more time we’re gonna get another number.
Copy this paste this down here and we’re gonna get this one for num2 so now we should have all of the input for our program so I’m getting the first number I’m getting the operator and I’m getting the second number the last.
Do now is actually do the math so we’re gonna have to figure out which operator they wanted to use right so we have this Opie variable and this is storing like a plus sign a minus sign a Asterix or a forward slash so depending on what that’s storing we want to print something different out so we can actually use an if statement to do this so I can use an if statement to check and see what operator is inside of our operator variable and depending on.
Which one it is we can do a different operation so I can say if and the first thing I’m going to do is just check to see if the operator is equal to a plus sign so if the operator that the user entered is equal to a plus.
Sign then we’re gonna want to.
Add the two numbers together so I’m just gonna print out these two numbers and I’ll just say num1 plus num2 because we’re gonna add both of them together we can also use an L safe so I’m going to want to check a few other conditions I’m going to check to see if the operator is equal to a minus sign and if the operator is equal to a minus sign then instead of adding the numbers we’re going to subtract them so.
Over here I’ll just copy this and we’ll paste this guy down here so it’s going to be num1 – num2 and i can actually just copy this whole thing and we’ll paste it down here so it.
Makes more room down here we’ll paste this other elsif and here we’re going to check to see if it’s division so we’ll check for a forward slash and again we’re just going to want to print out a decimal number so it’s going to be num1 divided by num2 finally we’re gonna check.
For multiplication so if it’s multiplication then we’re going to multiply them together so now we’re checking for each of the operators we’re checking for addition subtraction multiplication and division but here’s the thing what happens if the.
User enters in an operator that we didn’t want so for example if they don’t enter in any of these operators we’re gonna want to tell them so I’m gonna have this.