The goal for this exercise is to understand how to use the various compound assignment operators, and very common, but somewhat tricky, increment and decrement operators.
What you need to do to prepare for this exercise:
It is the instructor's opinion that many language features that exist in the C-derived family of programming languages (C, C++, Java, C#, JavaScript, etc, etc) exist because programmers hate to type. The compound assignment operators is one example of this, the increment/decrement operators are another example. For all of these types of operators, they don't really allow you to do anything new, but they do reduce the amount of typing that you'll need to do. For example, instead of writing
int num = 1; num = num + 10;
you can write
int num = 1; num += 10;
Similarly, instead of
int num = 1; num += 1;
you can write
int num = 1; num++;
For this exercise, you need to be familiar with all the following compound assignment operators:
+=
-=
*=
/=
%=
In the provided starter project, in the Compound_Operators.RunExercise
method, write
some code that demonstrates the usage of each of the above operators. The code doesn't have to
be any more complicated than the examples given in this document, for +=.
Also you should understand ++
and --
, and in particular, you should know
the difference between the 'prefix' and 'postfix' behavior. You should include (in a comment
in your C# code) clearly explaining (A) what the output of each of the following code snippets
is, and (more importantly) (B) why the behavior of ++
/--
causes the output to be different.
// ++ before num is 'prefix' int num = 0; while (++num < 10) Console.WriteLine("Num: {0}", num); // ++ after num is 'postfix' num = 0; while (num++ < 10) Console.WriteLine("Num: {0}", num); // -- before num is 'prefix' num = 10; while (--num > 0) Console.WriteLine("Num: {0}", num); // -- after num is 'postfix' num = 10; while (num-- > 0) Console.WriteLine("Num: {0}", num);
(feel free to simply paste the above code into the provided Increment_Decrement. RunExercise
method, and
then put your answers, in comments, in-line)