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 />

A First C# Program<br />

()<br />

This is a pair of brackets enclosing nothing. This may sound stupid, but actually tells<br />

the compiler that the method Main has no parameters. A parameter to a method gives<br />

the method something to work on. When you define a method you can tell C# that it<br />

works on one or more things, for example sin(x) could work on a floating point<br />

value of angle x. We will cover methods in very great detail later in this document.<br />

{<br />

This is a brace. As the name implies, braces come in packs of two, i.e. for every open<br />

brace there must be a matching close. Braces allow programmers to lump pieces of<br />

program together. Such a lump of program is often called a block. A block can contain<br />

the declaration of variables used within it, followed by a sequence of program<br />

statements which are executed in order. In this case the braces enclose the working<br />

parts of the method Main.<br />

When the compiler sees the matching close brace at the end it knows that it has reached<br />

the end of the method and can look for another (if any). The effects of an un-paired<br />

brace are invariably fatal....<br />

A double variable can hold a<br />

very wide range of values to a<br />

very high precision.<br />

double<br />

By now you probably feel that you need a drink. But that is not what double means in<br />

this context. What it means is "double precision floating point number".<br />

Our program needs to remember certain values as it runs. Notably it will read in values<br />

for the width and height of the windows and then calculate and print values for the<br />

glass area and wood length. C# calls the places where values are put variables. At the<br />

beginning of any block you can tell C# that you want to reserve some space to hold<br />

some data values. Each item can hold a particular kind of value. Essentially, C# can<br />

handle three types of data, floating point numbers, integer numbers and text (i.e. letters,<br />

digits and punctuation). The process of creating a variable is called declaring the<br />

variable.<br />

You declare some variables of a particular type by giving the type of the data,<br />

followed by a list of the names you want the variables to have. We are using the type<br />

double for now. Later we will use other types.<br />

width, height, woodLength, glassArea<br />

This is a list. A list of items in C# is separated by , (comma) characters. In this case it<br />

is a list of variable names. Once the compiler has seen the word double (see above) it<br />

is expecting to find the name of at least one variable to be created. The compiler works<br />

its way through the list, creating boxes which can hold values of type double and<br />

giving them the appropriate names. From this point on we can refer to the above<br />

names, and the compiler will know that we are using that particular variable.<br />

Programmer’s Point: Know where your data comes from<br />

In fact, given the limitations in the accuracy to which people can read tape measures, and the fact that we are not<br />

going to make any windows as wide as the universe, a double precision floating point number is overkill for this<br />

application. You would instead ask the customer if it is OK to just express the dimensions in millimetres instead. We<br />

will look at the considerations driving the choice of particular variable types a bit later on. All these decisions are<br />

driven by the metadata (data about data) that you gather when you are finding out about the system you are creating.<br />

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

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

Saved successfully!

Ooh no, something went wrong!