Subtraction is fairly straightforward in C#. To subtract one number
from another, you use the minus symbol ( - ).
Add another button to your form. Set the following properties for it in the
Properties Window:
Name: btnSubtract
Size: 100, 30>
Text: Subtract
Double click the button to get at the code. Add the following three lines of
code:Size: 100, 30>
Text: Subtract
int answerSubtract;
answerSubtract = 50 - 25;
MessageBox.Show( answerSubtract.ToString() );
Your coding window will then look like this:Run your code, and make sure it works. The answer you should see in the message box is, of course, 25. Stop your programme and return to the coding window. Now change the 25 to 25.5.
answerSubtract = 50 - 25.5;
Try to run your programme and you'll get the blue wiggly line, meaning there's
an error in your code. The reason is the same as for addition: we're trying
to place a float number into an integer variable (the answer will be 24.5, this
time). Just because the math symbol has changed doesn't mean we can disobey
the C# rules!Change it back to 25, and the code will run fine.
As with addition, you can subtract more than one number. Change your line to this:
answerSubtract = 50 - 25 - 10 - 2;
When you run your programme, you should see 13 in your message box, the answer
to the subtraction.You can also use variable names in your subtraction. Add the following integer variable to your code:
int numberOne = 12;
Then change the second line to this:
answerSubtract = 50 - numberOne;
Here's what your coding window should look like:Run you programme and click your button. You should see an answer of 38 in your message box.
Mixing Subtraction and Additionin C#
You can mix subtraction and addition. The process is quite straightforward.
What we'll do next is to add two numbers together, and then subtract a third
number from the total.
Add another button to your form. Set the following properties for it:
Name: btnMixed
Size: 100, 30
Text: Add and Subtract
(If you need to make your Form bigger, click on the form to select it. Then
change the Size property in the Properties Window.)Size: 100, 30
Text: Add and Subtract
Double click your button to get at the code. We'll need four integer variables for this. So set up the following:
int firstNumber;
int secondNumber;
int thirdNumber;
int answer;
To place values in the variables, add the following three lines:int secondNumber;
int thirdNumber;
int answer;
firstNumber = 100;
secondNumber = 75;
thirdNumber = 50;
Your coding window will then look like this:secondNumber = 75;
thirdNumber = 50;
answer = firstNumber + secondNumber;
Display the answer in a message box by adding this line:
MessageBox.Show( answer.ToString( ) );
When you run the programme and click your button, you should see the message
box display an answer of 175.Stop the programme and return to your code.
We now want to subtract the third number from the first two. So change this line:
answer = firstNumber + secondNumber;
to this:
answer = firstNumber + secondNumber - thirdNumber;
When C# sees all these variables after the equals sign, it will try to calculate
using the numbers you've stored in the variables. Usually, it will calculate
from left to right. So this bit gets done first:
firstNumber + secondNumber
When C# finishes adding the first two numbers, it will then deduct the value
in thirdNumber. The answer is then stored in the variable to the left
of the equals sign.Run your programme. When the button is clicked, the message box will display an answer of 125.
So mixing addition and subtraction is fairly straightfoward - just use the + and - symbols. However, there can be problems! In the next part, you'll about something called Operator Precedence.
0 comments:
Post a Comment