To subtract, you use the - sign. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% // Values used in this program double Value1 = 224.58, Value2 = 1548.26; double Result = Value1 - Value2; %> <% Response.Write(Value1 + " - " + Value2 + " = " + Result); %> </body> </html> This would produce:
To decrement a variable, you can subtract 1 from it and assign the result to the same variable. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% int Value = 12; Response.Write("Techniques of decrementing a value<br />"); %> <% Response.Write("Value = " + Value + "<br />"); %> <% Value = Value - 1; %> <% Response.Write("Value = " + Value); %> </body> </html> This would produce: To simplify this operation, you can use the -- operator. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% int Value = 12; Response.Write("Techniques of decrementing a value<br />"); %> <% Response.Write("Value = " + Value + "<br />"); %> <% Value--; %> <% Response.Write("Value = " + Value); %> </body> </html>
Once again, the position of the operator can be important. If you want to decrement the variable before calling it, position the decrement operator on the left side of the operand. If you plan to decrement a variable only after it has been accessed, position the operator on the right side of the variable.
You may want to subtract a constant value from a variable. To decrement a value from a variable, use the subtraction and apply the same technique. This is done with the -= operator. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% double Value = 12.75; Response.Write("Techniques of incrementing and decrementing a value"); Response.Write("<br />"); Response.Write("Value = " + Value + "<br />"); Value -= 2.42; Response.Write("Value = " + Value); %> </body> </html> This would produce:
To perform the division, you use the forward slash /. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% // Initializing various variables when declaring them with the same data type double Value1 = 224.58, Value2 = 1548.26; double Result = Value1 / Value2; Response.Write(Value1 + " / " + Value2 + " = " + Result); %> </body> </html> This would produce: When performing the division, be aware of its many rules. Never divide by zero (0).
Remember that you can add, subtract, or multiply a value to a variable and assign the result to the variable itself. You can also perform this operation using the division. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% double Value = 12.75; Response.Write("Value = " + Value + "<br />"); %> <% Value = Value / 2.42; Response.Write("Value = " + Value); %> </body> </html> This would produce: To perform this operation faster, you can use the /= operator. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% double Value = 12.75; Response.Write("Value = " + Value + "<br />"); %> <% Value /= 2.42; Response.Write("Value = " + Value); %> </body> </html>
The remainder operation is performed with the percent sign (%) which is gotten from pressing Shift + 5. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% int Players = 18; int Remainder = Players % 11; %> <% // When the game starts, how many players will wait?. Response.Write("Out of " + Players + " players, " + Remainder); Response.Write(" players will have to wait when the game starts."); %> </body> </html> This would produce:
As seen with the other arithmetic operators, you can find the remainder of a variable and assign the result to the variable itself. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% int Players = 18; // When the game starts, how many players will wait?. Response.Write("Out of " + Players + " players, "); %> <% Players = Players % 11; %> <% Response.Write(Players); Response.Write(" players will have to wait when the game starts."); %> </body> </html> To support a faster version of this operation, you can use the %= operator. Here is an example: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <% int Players = 18; // When the game starts, how many players will wait?. Response.Write("Out of " + Players + " players, "); %> <% Players %= 11; %> <% Response.Write(Players); Response.Write(" players will have to wait when the game starts."); %> </body> </html> |
|
|||||||||||||||||||||||
|