Basic Arithmetic Operators

advertisement
Performing Simple Arithmetic in Your Programs
The goal for this exercise is to understand how to use simple arithmetic operators.
In the previous exercises, the assignment statement was introduced. For example,
x = 5;
assigns 5 to the variable x.
It is also possible to have more complicated mathematical expressions on the right-hand side of
an assignment statement:
x = (15 – 5) / 2;
The statement above also assigns 5 to x, using a more complicated expression to do so.
Here is a list of some operators used to perform arithmetic in C#:






()
*
/
%
+
-
Used to group operations
Multiplication
Division
Modulus
// we'll look at modulus in more detail in class
Addition
Subtraction
Here is part of the starter project that was provided to you for this set of PCEs (pre-class
exercises), that uses these operators:
class Basic_Arithmetic_Operators
{
public void RunExercise()
{
{
int x = 2, y = 8, z;
Console.WriteLine("2
Console.WriteLine("8
Console.WriteLine("8
Console.WriteLine("2
Console.WriteLine("8
times 8 equals " + x * y); // multiply
divided by 2 equals " + y / x); // divide
mod 2 equals " + y % x);
// modulus
plus 8 equals " + (x + y));
// add
minus 2 equals " + (y - x));
// subtract
z = ((y - (2 * x)) * 6) / 12;
Console.WriteLine("z now equals " + z);
}
}
}
You’ll also notice that the declaration in this program is a little different:
int x = 2, y = 8, z;
This declares three variables, an integer named x (which is immediately assigned the value 2),
an integer named y (which is immediately assigned the value 8), and an integer named z. You do
not have to immediately assign an initial value, but it is an option.
You can declare as many variables as you want with a single declaration, as long as the variable
names are separated by commas and the statement ends with a semicolon. All of the variables
will be of the same type – the type that begins the declaration (in this case, int).
You’ll notice in the program above that there are some statements that are followed by a pair of
slashes, //, and some descriptive words:
Console.WriteLine("2 plus 8 equals " + (x + y) );
// add
These are called comments. A pair of slashes indicates that the remainder of the line is to be
ignored. You are then free to put descriptive remarks following the slashes.
Comments can make programs easier to understand by providing an English description of what
is happening.
Comments can also be used to ‘disable’ certain lines of code in your program. For example, in
the starter project, you’ll notice that in main (where all C# programs start), there are two lines of
code that initially look like:
// Basic_Arithmetic_Operators bao = new Basic_Arithmetic_Operators();
// bao.RunExercise();
By ‘commenting out those lines’, we have disabled those lines (so they won’t be run by our C#
program), but we’ve kept the lines in our program (so we can put them back in later, if we want).
Putting them back in the program is called ‘uncommenting’, and simply means that you delete
the two // , so that the lines will again be compiled & run.
Uncomment the above two lines of code in the starter project so that it will run the
Basic_Arithmetic_Operators . RunExercise method, rebuild and rerun it. What happens?
What you need to do for this exercise:
1. You should have read everything above, understood everything above, and tried out all the
code, on your own, so that you’re clear about how it works. You are also encouraged (as you
always are) to ‘play around’ with this, as well – try stuff that isn’t specifically called for
above, just to test your knowledge, and just to see if you can.
2. Other than that, there are no particular requirements for this exercise.
Download