Basic Program Structure, and Flow Control

Before we get too much further in C# programming, I should probably talk at least a little bit about basic program structure and key words we can utilize to control the flow of a program.

Let’s go ahead and create yet another new Console Project via File -> New -> Project -> Visual C# -> Console Application. Name it whatever you like (I have named mine “ProgramStructure”).  On the right hand side (by default) you will notice a tree heading called References.  Go ahead and expand this and you will see a list of references that are linked to your program (and the .dll files associated with these references):

References

You may have noticed that in the first “Hello World” tutorial, we typed the following:

System.Console.WriteLine("Hello World!");

Now take a look at the very top of your newly created Program.cs file. You should see a series of statements like using System; By including this statement, we eliminate the need to fully qualify how we are accessing the Console class. That is, we could easily have just written:

 Console.WriteLine("Hello World!"); 

The using statements you see are provided by default (and change depending upon the project type), but, to be honest these will probably not mean much to you for quite some time. Never fear! We will use them to our advantage later.

Below the using statements, you should see namespace <NameofProject> or, in my case, namespace ProgramStructure. A namespace allows us to qualify completely a method (function) or property that we are attempting to access. For instance, imagine we had written our own version of the Console class–if we were to try and access the Console how would the program know which we were referring to? The using statements covered earlier represent a set of included namespaces (default).

After the namespace declaration, we have the class name that the file (Program.cs) is defining. For a simple console application this doesn’t have much weight, but this will become very important later especially when we start to delve into OOP (Object Oriented Programming) concepts. It is important to know however, that everything in C# is a class.

Try renaming your “Program” class by changing class Program to class whateveryoulike. Will the program still run? You bet! Again, don’t sweat these definitions if you haven’t got the hang of them yet–you will in time!

Inside the class declaration we have a simple function called Main that accepts an array of strings as arguments (potentially). This is the entry-point for the application. Know that we can define any number of methods and properties inside the class declaration, and any number of classes can exist inside a given name space. Organizationally any C# program can be broken down to:

  • Namespaces -> Classes -> Methods, Properties etc

As for controlling the flow of a program, I will cover a few basic concepts:

  • if/then, else statements (This time)
  • switch/case statements (Next time)

If, Else

Say we wanted to test to see if a given player was brain dead or not using a simple console program.  We can do that!  Let’s tart by asking the user (by printing some dialog to the Console) to enter in the number ‘3’.

Console.WriteLine("Hello Adventurer!  Please enter the number '3' to continue!");
Console.ReadLine();

If you run the program now, it kind of sucks! Let’s use Visual Studio’s Intellisense (I will cover this more in detail later) to aid us in seeing what we can do with Console.Readline(). Hover over the ReadLine word and you should see the following pop up:

intellisenseThe keyword on the far left string, tells us what that function returns! We know what a string is! Let’s change the code to the following so we can store the user’s data into a string:

Console.WriteLine("Hello Adventurer!  Please enter the number '3' to continue!");
string response = Console.ReadLine();

Now let’s use an if/then statement to write a message to the screen that is dependent upon the user’s input:

Console.WriteLine("Hello Adventurer!  Please enter the number '3' to continue!");
string response = Console.ReadLine();

if (response == "3")
{
     Console.WriteLine("Congratulations, you are not brain dead!");
}
else
{
     Console.WriteLine("You suck.");
}

Console.ReadLine();

Now, run the program a few times and test to see if your program behaves as you would expect. With the following structure we are telling the program to look at the response given by the user (== indicates an evaluation for equality, while = simply assigns a value), and respond one way if the number “3” is entered. The program will respond in a different kind if ANY other value is entered instead of JUST 3. Remember that the last Console.ReadLine() call is only there to keep the console window from immediately closing.

As an alternative, you can evaluate multiple conditions by including an else if statement.

Say we wanted to modify the program to accept the number 3 or 7:

Console.WriteLine("Hello Adventurer!  Please enter the number '3' or '7' to continue!");
string response = Console.ReadLine();
if (response == "3")
{
     Console.WriteLine("Congratulations, you are not brain dead since you entered 3!");
}
else if (response == "7")
{
     Console.WriteLine("Congratulations, you are not brain dead since you entered 7!");
}
else
{
     Console.WriteLine("You suck.");
}
Console.ReadLine();

There’s a simpler way to evaluate multiple boolean expressions (expressions that return either true or false), which we will cover later!  For now, keep coding!  See if you can modify the program to have a player enter their name, and then have the program write it to the screen!


Leave a comment