Assignment statements



Assignment statements and arithmetic in C#Assignment statementsAn assignment statement assigns a value to a variable. Assignment statements in C# are almost (there's a semicolon on the end) identical to those in Python. The syntax is:variable = value;However, unlike python, the variable must have been declared before it can be used, like this:string name;int age;Then we can assign values to the variables, like this:name = "George"; // Assign the text string "George" to the variable nameage = 40; // Assign the integer value 40 to the variable ageThe type of data on the right side must be the same as the type of the variable on the left side. The following are illegal:age = 40.5;// 40.5 is a float/double, not an integerage = "40";// "40" is a string, not an integerage = "George";// "George" is a string, not an integername = 25;// 25 is an integer, not a stringname = 40.5// 40.5 is a float/double, not an integerStructured dataSome variables can hold more than one value. When this happens, we use "dot" notation to identify which value we are referring to.The Color data typeA Color variable holds 3 values (each of which is a byte and can hold a value from 0 to 255): R, G, and B (and there is also a fourth called A, but we won't be using it). If we declare a Color variable like this:Color backgroundColor, foregroundColor;Then we can refer to the components of the backgroundColor variable like this:backgroundColor.R = 0;backgroundColor.G = 0;backgroundColor.B = 0;The data type of backgroundColor is Color. If backgroundColor appears on the left side of an assignment statement, a Color constant or variable must appear on the right. Like this:backgroundColor = foregroundColor;The data type of backgroundColor.R is byte. If backgroundColor.R appears on the left side of an assignment statement, a byte constant or variable (value in the range 0-255) must appear on the right side. Or, you can use type-casting to convert the value on the right side to a byte.backgroundColor.R = (byte) 255; // the (byte) is required because the type of 255 is integer, not bytebackgroundColor.R = foregroundColor.R;The data type of backgroundColor.G is byte. If backgroundColor.G appears on the left side of an assignment statement, a byte constant or variable (value in the range 0-255) must appear on the right side. Or, you can use type-casting to convert the value on the right side to a byte.The data type of backgroundColor.B is byte. If backgroundColor.B appears on the left side of an assignment statement, a byte constant or variable (value in the range 0-255) must appear on the right side. Or, you can use type-casting to convert the value on the right side to a byte.The Vector2 data typeA Vector2 variable holds two values: X and Y. If we declare a Vector2 variable like this:Vector2 nameVector;then we can refer to the components of the position variable like this:nameVector.X = 400; // halfway acrossnameVector.Y = 240; // halfway downThe data type of nameVector is Vector2. If nameVector appears on the left side of an assignment statement, a Vector2 variable or constant must appear on the right side.The data type of nameVector.X is float. If nameVector.X appears on the left side of an assignment statement, a float variable or constant must appear on the right side.The data type of nameVector.Y is float. If nameVector.Y appears on the left side of an assignment statement, a float variable or constant must appear on the right side.The Rectangle data typeA Rectangle variable holds four values: X, Y, Width, and Height.If we declare a Rectangle variable like this:Rectangle ufoRectangle;Then we can refer to the components of the ufoRectangle variable like this:ufoRectangle.X = 400;// halfway acrossufoRectangle.Y = 240;// halfway downufoRectangle.Width = 100;// 100 pixels wideufoRectangle.Height = 50;// 50 pixels tallThe data type of ufoRectangle is Rectangle. If ufoRectangle appears on the left side of an assignment statement, a Rectangle variable or constant must appear on the right side.The data type of ufoRectangle.X, ufoRectangle.Y, ufoRectangle.Width, and ufoRectangle.Height is int (integer). If any of these variables appears on the left side of an assignment statement, an int variable, constant, or expression must appear on the right side. Or you must use type-casting to convert the value into an int.ArithmeticWe move things on the screen by changing their X and/or Y coordinate and then drawing the item again.Incrementing a variableIf we want to increase the value of an integer variable by 1 (e.g. to move it across the screen), we can use the "++" operator. If we have a UFO in the ufoRectangle on the screen, the following will move him one pixel to the right:ufoRectangle.X++;And the following will move him one pixel down:ufoRectangle.Y++;Decrementing a variableIf we want to decrease the value of an integer variable by 1 (e.g. to move it across the screen), we can use the "--" operator. If we have a UFO in the ufoRectangle on the screen, the following will move him one pixel to the left:ufoRectangle.X--;And the following will move him one pixel up:ufoRectangle.Y--;Increasing a variable by more than 1If we want to move something more than one pixel, we have to add more than 1. The following will move our UFO 5 pixels to the right using the "plus-equals" operator:ufoRectangle.X += 5;This (above) is actually a shortcut for this:ufoRectangle.X = ufoRectangle.X + 5;And we can use variables instead of constants:int ufoXSpeed = 5; // in the declaration area at the topufoRectangle.X += ufoXSpeed;And this will move him 5 pixels down:ufoRectangle.Y += 5;The "+=" operator means "add the value on the right to the variable on the left."Decreasing a variable by more than 1There is also a "-=" (operator that will subtract a value from a variable. The following will move our ufo 5 pixels to the left:ufoRectangle.X -= 5;And this will move him 5 pixels up:ufoRectangle.Y -= 5;Arithmetic operatorsArithmetic operators in C# are similar to those in Python:+ is Addition- is subtraction* is multiplication/ is division% is mod (remainder)However, there is no // operator. In C#, if both operands are integers, the operator does integer division. If one of the operands is a floating point number, then the result is a floating point number (double).There also is no exponentiation operator in C#. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download