Blogger templates

Pages

Tuesday, 22 October 2013

Subtraction in C# .NET

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:
int answerSubtract;
answerSubtract = 50 - 25;
MessageBox.Show( answerSubtract.ToString() );
Your coding window will then look like this:
Subtraction code in C# .NET
So we've set up an integer variable called answerSubtract. On the second line, we're using the minus symbol to subtract 25 from 50. When C# works out the answer to 50 - 25, it will place the answer to the left of the equals sign, in the answerSubtract variable. On the final line, we're displaying the answer in a message box.
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:
A new variable has been added
What we're doing here is setting up an integer variable called numberOne. We're then placing a value of 12 inside of the variable - all on the same line. For the second line, we're subtracting whatever is in the variable called numberOne from 50.
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.)
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:
firstNumber = 100;
secondNumber = 75;
thirdNumber = 50;
Your coding window will then look like this:

Three integer variables
To add the first number to the second number, and then place the result in the variable we've called answer, add the following line to your code:
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