22.02.2016 Views

C Programming Yellow Book

6019BjHWX

6019BjHWX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Simple Data Processing<br />

Manipulating Data<br />

The convention in C# for the kind of variables that we are creating at the moment is to<br />

mix upper and lower case letters so that each word in the identifier starts with a capital:<br />

float averageIceCreamPriceInPence;<br />

This is sometimes called camel case, presumably because of the "humps" in the<br />

identifier which are caused by the capitals.<br />

Programmer’s Point: Think about the names of your variables<br />

Choosing variable names is another skill you should work at. They should be long enough to be expressive but not so<br />

long that your program lines get too complicated. Perhaps the name averageIceCreamPriceInPence is a bit over the<br />

top in this respect. But I could live with it. Remember that you can always do sensible things with your layout to make<br />

the program look OK:<br />

averageIceCreamPriceInPence =<br />

computedTotalPriceInPence / numberOfIceCreams;<br />

2.2.6 Giving Values to Variables<br />

Once we have got ourselves a variable we now need to know how to put something<br />

into it, and get the value out. C# does this by means of an assignment statement. There<br />

are two parts to an assignment, the thing you want to assign and the place you want to<br />

put it, for example consider the following:<br />

class Assignment<br />

{<br />

static void Main ()<br />

{<br />

int first, second, third ;<br />

first = 1 ;<br />

second = 2 ;<br />

third = second + first ;<br />

}<br />

}<br />

Code Sample 02 Silly Assignment Example<br />

The first part of the program should be pretty familiar by now. Within the Main<br />

function we have declared three variables, first, second and third. These are each<br />

of integer type.<br />

The last three statements are the ones which actually do the work. These are<br />

assignment statements. An assignment gives a value to a specified variable, which<br />

must be of a sensible type (note that you must be sensible about this because the<br />

compiler, as we already know, does not know or care what you are doing). The value<br />

which is assigned is an expression. The equals in the middle is there mainly to confuse<br />

us, it does not mean equals in the numeric sense; I like to think of it as a gozzinta (see<br />

above). Gozzintas take the result on the right hand side of the assignment and drop it<br />

into the box on the left, which means that:<br />

2 = second + 1;<br />

- is a piece of programming naughtiness which would cause all manner of nasty errors<br />

to appear.<br />

Expressions<br />

An expression is something which can be evaluated to produce a result. We can then<br />

use the result as we like in our program. Expressions can be as simple as a single value<br />

and as complex as a large calculation. They are made up of two things, operators and<br />

operands.<br />

C# <strong>Programming</strong> © Rob Miles 2015 28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!