Chapter 1: An Introduction to Programming
In this chapter we'll start programming. We'll start at the very beginning, discussing the very basics of what programming is and the different paradigms of programming languages. Then we'll make our first C# program. Specifically we'll do the following:
l Learn the basic concepts of programming
l Look at the different types of programming languages available
l Setup Visual C# Express and XNA 3.1
l Create a simple C# program
Programming Basics
So what exactly is a computer program? We can say that a computer program is just a list of instructions written by someone (a programmer) that can be given to a computer. The instructions (called computer code or just code) are written in a programming language that the computer can understand, and when the instructions run on the computer we say the program is being executed.
You may have heard before that computers understand nothing except ones and zeros and that's true. The language of computers is binary, which is just long lines of zeros and ones. Everything you see, all the cool artwork in the latest games, comes down to strings of binary. In the very early days of computers people worked with binary directly, trying to input representations of ones and zeros into the computer somehow. But binary, while easy for computers to understand, is difficult for people, so computer scientists started making special letter combinations to represent different computer commands from the binary. For instance, let's say the programmer wanted to tell the computer to add two numbers. The numbers are stored in memory at places called R1 and R2 and the programmer wants to put their sum at place R3, the programmer would input something like this:
ADD R1, R2, R3
This is called assembly language and for years computer programs were written in this, and all of the video games were programmed with it (and it is still used in graphics today in shaders.) But writing computer programs with assembly is still not very straightforward. Seeing one or a few lines of assembly code is simple enough but imagine trying to deal with thousands of lines of it. This lead people to create high level programming languages like C++, Java, or C#. These languages let people program computers in a way that is easier for the programmer to understand. For example, the previous line of assembly code would be written like this in C#:
r1 = r2 + r3;
Except that instead of r's for the names of the numbers we'd have something more meaningful, like:
totalGold = goldInBag+ goldInTreasureChest;
This is a statement, a line of computer code. The files that contain code are called source files. With a high level language we need a way to convert these high level statements into instructions that the computer can understand. To do this we use a program called a compiler. A compiler takes in source files and changes the instructions to a form the computer can understand (which isn't binary directly; it changes to other code for the operating system, but we won't worry about those details.) The compiler creates an executable program that can be run on the computer. This process of taking the source file(s) and creating the executable is called compiling or building the program. The compiler we are going to use is Visual C# Express, a very popular and powerful compiler from Microsoft, and we'll go over setting it up later in this chapter.
As for the programming language we'll use C# (pronounced C sharp.) It is our language of choice for, among other reasons, it's the only language that works with XNA. C# is only one of many other programming languages (some of the most popular are C++, Java, and Visual Basic.) They each have their own strengths and weaknesses. C# is similar to Java and the language C++ (and C++ is an extended version of the C language.) Some beginners, when hearing that C# was partially based off of C++, think it's a good idea to first try to learn C++ or C before C#. But that is really unnecessary, and can make things more confusing trying to learn two or three languages instead of one. C# is actually a lot easier to use than C++ (at least in my opinion) and there's no advantage to learning C++ if you're working with C#. (But note that currently C++ is the most popular language to program games. While C# may become the dominant language, today all of the larger and powerful games use C++, so it would be good to learn C++ after C#.)
We should also note that C# uses the .Net framework; a huge collection of classes that provide functionality for a wide range of applications. We'll also be using XNA 3.1. XNA is a special add-on that has game programming functionality which obviously we'll learn a lot about later. But before getting into much info on C# specifically, let's look a bit at programming languages in general.
Types of Languages
Not all programming languages are created equal, and it's good to know about the different types of languages available. While all computer code eventually turns into binary for the computer, before that they have a lot of differences. The most obvious difference between various programming languages is their syntax, the rules for how they are written. Syntax is an important issue for programming, as all computer instructions must be typed in a very strict way, the littlest change from the way a computer statement is supposed to be will give an error. For instance, part of C#'s syntax is that at the end of each instruction there is a semicolon (like at the end of the adding example before) and without the semicolon an error will result. This semicolon rule is pretty common, but some languages like Visual Basic don't use a semicolon for that. But besides syntax differences computer languages can also differ in the way they are designed; their whole way of being organized and structured. There are only a few major paradigms (types) of programming languages. Let's look at a few.
Structured (Procedural) Languages
Structured languages are written in a very straightforward way. They start at the beginning and just list all of the computer instructions one right after the other. A program is made up of a (usually) very long sequential lists of commands telling the computer what to do. The computer executes (does) the instructions in order, doing statement one first, then statement two, and so on. A structured program looks something like this:
instruction 1;
instruction 2;
...
instruction 5000;
...
If you've ever taken a course in school or tried programming in BASIC, then you've used a structured language. This was the original and major paradigm for computer languages for years, and is still popular today. The most popular language of this type is called C (and C#'s syntax is based on it.) Looking at our program above again, you can imagine that as these types of programs become very long you'll need to repeat some of the code. Let’s say in several parts in a program we need to convert a temperature from Celsius to Fahrenheit and that code will be repeated over and over. To handle this structured languages are usually block based, meaning instead of one long list of code they have functions (blocks) of code that each perform separate tasks. The program for the temperature could look like the following:
function changeTemperature
(some instructions here)
function end
instruction 1;
changeTemperature;
instruction 2;
...
The temperature conversion function is defined in the beginning of the program, and after instruction 1 we call the function changeTemperature and do all of the code inside of it. Then we continue with other code and will call it again later when needed. This has many big advantages. The main one being we don't have to repeat typing in computer code; we can write something once and then call it as a function when we need to. Using functions also helps organize the code; we can put separate functions together in separate source files. Functions also help with a host of other issues we'll look at later.
But structured programming does have disadvantages. For starters, having all of these functions is fine up to a point but as programs become very large they can start to be confusing, and errors in the code can be hard to find. But more importantly, structured programs are written in a way for computers to understand, not for humans to understand. If we started designing, say, a car game we would start describing it in ways like see a car on screen, press up to accelerate, down to break, have to hit this checkpoint before time runs out, etc. When we go to write a structured program to do this it's not always easy to change that design to a linear list of code. The game isn't thought of as just a series of steps, and it'd be nice if our programs could be coded in a way that reflected that. So developers came up with our next type of programming language:
Object-Oriented Languages
Instead of looking at the car game and asking what series of steps we can do to describe a car in a computer program, it would be nice if we had a programming language that could describe a car in more human terms. Instead of thinking of steps to program a car we would like to program a car game by creating a car object and giving it a description. Object-Oriented programming (OO) does just that. Instead of thinking of a series of instructions for a car, we could first think how we can describe a car, what model is it, what color, or describing how fast it's going. We can then think of what kind of functionality a car has, such as we can accelerate a car, turn it, or brake, etc. We can treat the car as an object, and OO programming allows us to think of the real world and program in terms of objects. We make classes of objects, which are descriptions for types of objects. Classes are designs for objects, a template for what they are like. The actual objects themselves are called instances of a class. For example, we could make a very specific car class to describe Honda Accord cars, the class would contain information all Accords have (such as year, mileage, color, etc) But this class would only described the car in general, an actual Honda Accord sitting in a parking lot outside of the building I'm in right now would be an instance of the Honda Accord class. The car in the parking lot is not a class of cars; it is a specific instance of a car. This difference between classes/instances may not seem that important now but will become an important issue later.
Anyways, let's look at making just a general simple car class. We can create a class like the following:
class Car
{
modelType;
currentSpeed;
accelerate();
turnLeft();
turnRight();
brake();
}
Classes are made out of a combination of data (descriptive properties like the modelType and currentSpeed) and functions (actions that do something to the object like the accelerate() or turn left(). We can tell what is a function because they end in the two parentheses, (). Some people think of objects as nothing more than just data and functions, only a good way to organize programs. Others say that the objects in programs should be very robust and mimic real life, I've heard OO proponents say they're angry over how the term “window” is used in computers, as a window is something transparent you see through and that doesn't match up with its computer counterpart. Object-Oriented Design, choosing what the different objects and classes are going to be for a program, is huge field. In our programs and games, though; we'll just try to keep them as straightforward as possible.
Overall OO programming is the main paradigm in programming today. All of the most popular languages, C#, C++, Java are Object-Oriented, and even languages like Visual Basic which used to not be OO are becoming that way. There are just so many advantages to this way of programming. Besides it being easier to understand and organize programs, OO programming lends itself to be used by teams since different team members can easily do different objects for a system. We've just scratched the surface of OO programming here, but we'll look at it in more detail later in the book.
Lists/Logic Languages
Structured Programming and OO programming are the two major paradigms of programming, all of the very popular languages fall into one of them. But this doesn't mean that all programming languages work that way. Some languages work in very unique and original ways. For instance, the language SML uses lists as its way of programming. You start with a list (a string of characters) and this list is manipulated by the program as it goes through it ending with a different list at the end (if this sounds strange or confusing don't worry about it, as most programmers have a hard time understanding languages that aren't structured or OO) Other languages use logic to make programs, inputting in logical rules from philosophy to create programs. These other paradigm languages are usually very good at doing a few things (For instance, SML can do complex list manipulations much easier than C# or C++ ) but aren't general enough to do general programming tasks. And outside of making a few tools for making games, aren't used in the industry.
Type of C#
So we've been through the major types of programming languages, but what is C#? Well earlier we did say that C# is Object-Oriented and it is, everything in C# is an object, and all the code is about manipulating objects. It uses the .Net framework, which is a huge collection of classes (objects) to use. Likewise XNA is a collection of classes to use too, which are made especially for game programming. But to be more specific, C# is called a block-structured Object-Oriented language, which means that while objects are used for everything, the functions (or methods to be more precise, methods are a special type of functions), are contained in blocks, which seem like little procedural programs. In fact, the programs we'll be doing in the first part of the book will seem very similar to procedural programs, until we get to the part on OO.
Our First Program
But enough theory, let's get down to making our first program.
Setting up C# and XNA
Before we can program anything we'll need to set up our programming environment, that is to say Visual C# Express and XNA. I could go through the steps to setting up XNA in detail, but Microsoft has a lot of good documentation set up for this; we’ll just mention the main points. For details go to: creators.xna.com/en-US/quickstart_main. Also note if you have a full commercial version of Visual Studio you can use that instead of the Express edition.
To setup XNA download and install the following:
· Visual C# 2008 Express Edition
· XNA Game Studio 3.1
Installing these is simple, just go to creators.xna.com/en-US/downloads and click on the appropriate links. Again go to creators.xna.com/en-US/quickstart_main for more details.
First C# Program
Now that we have Visual C# Express setup let's go straight to making our first program. Go ahead and start Visual C# by going to Start->All Programs->Microsoft Visual C# Express 2008. You'll see a screen similar to the figure below.

Our first program is the traditional first program of any programming language; we'll make a program that prints “Hello World!” on the screen. That may not sound like the most exciting thing out there, but it's an accomplishment to create a program and execute it.
Our first step is to create a project. All code in C# is organized into projects, which organize source files and resources. In Visual C# Express Go to File->New Project. There are several program types, but select the icon Console Application, and in the name type “HelloWorld” (without the quotation marks).

Then click OK. The code below is then displayed in the left side of the window:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
}
}
}
We'll go through this line by line in a moment, but for now just type in the following line in between the two brackets {} after the string void Main line:
Console.WriteLine("Hello World!");
The complete program should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Next go to Debug->Start Without Debugging (or hit Ctrl+F5). The following will display on the screen:

Simple as that we have our first program running. If you have any problems make sure the code is typed in exactly as in the listing. Let's go through it line by line, starting with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
C# gives a lot of different instructions we can use in the program, but we want to tell C# which of the instructions we'll be using. (We don't want C# to give us every available instruction, since we won't use most of them and it adds overhead to the program to say we'll use more instructions then we need.) The first part of the program with the “using” statements tells C# what instructions we'll use and we'll leave this the same for our Console programs, but include XNA references when we get to part two.
The”using” instructions also let us type things in more compactly. In the Console instruction we put in above we could have written:
System.Console.WriteLine("Hello World!");
The using System line allows us to write the Console command without the System.
Note again that each of the commands ends with a semicolon; it is a way of telling C# that an instruction is complete. The amount of whitespace (spaces, tabs, returns) after an instruction doesn't matter, that the semicolon is there is all that counts.
Another note is that C# defines blocks of code by brackets {}. Looking at our code again notice everything is in three blocks. The first block, namespace HelloWorld, defines a namespace, which is just a general way to group code. The second block class Program defines an object, like what we talked about briefly in the Object-Oriented programming section before and we'll discuss objects more later in the book. The final block “static void Main(string[] args)” defines a function (or method) called Main, and is the starting point for the program. When we start the program the compiler starts running instructions from Main (and no matter how large a program becomes it only has one Main.)
As for the line of the program that does the actual work:
Console.WriteLine(“Hello World!”);
Is a pretty straightforward instruction to the Console object (the box that holds the text) telling it to write the line “Hello World!” on the screen. Statements like these are the working part of programs, and they are contained in object's functions (methods). And that's the whole program.
Comments
Besides regular code instructions we can add comments to programs. Comments are lines of text that are helpful notes to the programmer but are completely ignored by the compiler. C# has support for three types of comments. The first is a single line comment, helpful for little descriptions. It is merely two slashes, //, and everything after them is ignored:
// Print out Hello World!
Console.WriteLine("Hello World!");
The second type of comment is a multi line comment; these are helpful for giving longer descriptions of things. They are used by /* for the beginning and */ for the end, everything in-between being commented:
/*
Print out Hello World!
*/
Console.WriteLine("Hello World!");
The third type of comment is for documenting code with XML, which is good, but we won't bother with it here for our programs.
Summary
In this chapter we had an introduction to computer programming and the different paradigms of programming languages out there. Then we setup Visual C# Express and created our first program. In the next chapter we'll look at programming more by seeing how programs handle data.
