Friday, March 25, 2011

Learn Gaming in c sharp Chapter 4

Typecasts:

Check out this code:

float a = 1.875;

int x = (int)a; // 1

Look at the last line: the value of a is 1.875, a fractional number, and the last line of code

is trying to put the value of a into x, which is an integer. Obviously, you can’t just transfer

the contents of a into x, so you need to lose some precision. Older languages, such as

C/C++, would do this for you automatically, and chop 1.875 down to 1 in order to fit it

into the integer (the process is called truncation). If you tried typing this line into a C#

program, however, you would get a compiler error:

x = a; // error! Cannot implicitly convert type ‘float’ to ‘int’

Of course, this code works perfectly well in older languages, so a lot of people will automatically

dismiss C# as “difficult to use.” I can hear them now: “Can’t you just automatically

convert the float to the integer, you stupid compiler?”

Well, the compiler isn’t actually stupid; it’s trying to save you some time debugging. You

may not realize it, but a common source of bugs in programs is accidental truncation.

You might forget that one type is an integer and some important data may get lost in the

translation somewhere. So C# requires you to explicitly tell it when you want to truncate

data. Tables 2.5 and 2.6 list which conversions require explicit and implicit conversions.

Explicit/Implicit Conversions, Part 1

From byte sbyte short ushort int uint

byte I E I I I I

sbyte E I I E I E

short E E I E I E

ushort E E E I I I

int E E E E I E

uint E E E E E I

long E E E E E E

ulong E E E E E E

float E E E E E E

double E E E E E E

decimal E E E E E E

 Explicit/Implicit Conversions, Part 2

From long ulong float double decimal

byte I I I I I

sbyte I E I I I

short I E I I I

ushort I I I I I

int I E I I I

uint I I I I I

long I E I I I

ulong E I I I I

float E E I I E

double E E E I E

decimal E E E E I

The above may look confusing at first, but they are actually quite simple. For example, if

you want to convert from an int to a double, look at Table 2.2, find “int” on the left and

find “double” on the top. In that position is an I, meaning you can perform an implicit

conversion:

int a = 10;

double b = a; // ok

Now say you want to convert a double to an int. Look at Table 2.1, find “double” on the

left and “int” at the top. There is an E at that place, which means you need to perform an

explicit conversion:

double a = 10.0;

// int b = a <—- ERROR

int b = (int)a; // ok

note

Converting from a float or a double to a decimal requires an explicit cast. This is because decimals

encode data in a different way than do floats or doubles, so there is a distinct possibility of losing

some data when performing the conversion. It’s probably nothing that we game programmers

should be concerned with, but you should be aware of it.

Branching

If you’ve really studied programming languages, then you know that there are three different

traits that a language must have to be considered a true programming language.

They are

_ Sequencing

_ Branching

_ Repetition

You’ve already seen the first trait, sequencing, in action in Demo 2.1. Sequencing essentially

means that the language must be able to execute commands in a given sequence.

Now I want to cover conditional statements, the use of which is known as branching.

Essentially, branching allows a computer program to look at a given set of variables and

decide whether it should continue executing or should branch to a different part of the

program.

C# has a few conditional statements built in to the language; all of them were inherited

from C, so you may be familiar with them.

if Statements

Quite often in a program, you will want to test to see if a condition is true or not, and then

take action depending on the outcome. For example, if you wanted to perform an action

if a condition evaluates to true, then you would write some code like this:

if( x == 10 )

{

// do something

}

The code checks to see if some variable named x has the value of 10, and then executes the

code inside the brackets if, and only if, x is 10. This is called an if statement.

You can also add on an else clause at the end, in order to execute code in any case where

x is not 10:

if( x == 10 )

{

// do something

}

else

{

// do something

}

So the computer executes everything in the first block when x is 10, and executes anything

in the second block when x is anything but 10.

Furthermore, you can chain elseif statements to the end, to perform multiple inquiries:

if( x < 10 )

{

// do stuff if x < 10

}

else if( x < 20 )

{

// do stuff if 10 <= x < 20

}

else if( x < 30 )

{

// do stuff if 20 <= x < 30

}

note

If you’re used to a language like C++, then you know you can use numbers inside of a conditional

to produce code like this: if( x ), where x is an integer. In older languages, the computer treats 0

as false and anything else as being true, meaning that if x is 0, then the if block won’t execute,

but it will for anything else. C# isn’t like this, however, and it actually requires you to use a Boolean

inside all conditional expressions. So the code will give you a compiler error in its current form.

When you think about it, the old way isn’t really safe anyway, because it doesn’t explain exactly

what you are testing. C# makes your programs safer and more readable.

Switch Statements

Using switch statements is a handy way to compare multiple outcomes of a single variable

quickly. For example, if you have a variable, x, that variable can hold the values of 1, 2, 3

and 4, and your program will take a different course of action for each value.You can code

a switch statement to do this:

switch( x )

{

case 1:

// do something if 1

case 2:

// do something if 2

case 3:

// do something if 3

case 4:

// do something if 4

default:

// do something if something else

}

So if x is 2, then the code will jump to the case 2 block, and so on.

There is a catch, however. In the current state of the code in the previous block, if x is 2,

then the code will jump right to block 2, but it will also continue on and execute the code

in every block below it. This means that the code will execute code block 3, 4, and default

as well. Sometimes you may want this behavior, but most of the time you won’t, so you

need to use the break keyword to break out of the switch after each block:

switch( x )

{

case 1:

// do stuff

break; // jump out of switch

Branching 25

case 2:

// do stuff

break; // jump out of switch

default:

// do stuff

break; // optional here

}

t i p

The break in the last block of the switch statement is optional, of course, because there is no code

below it. But it’s always a good idea to include the break anyway—just in case you end up adding

more blocks later on and forget to add in the last break.


No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...