<< Back

Logic

While technically the while loop from the first lesson introduced logic, as it does an action based on a value, this time we're going to expand on that a bit and help you get to the point where you really are programming!

Intermission

Before we move on, lets take a moment to look around a bit. You've probably noticed a few things while doing the first lesson: The auto-completion and hints when you type Console., for instance. Another thing to look at is what happens on mouse-over on various things. If you hover over the keepGoing variable, you'll see the word bool hilighted before the name. This tells you that the variable is of the boolean type, which can hold true or false. If you hover over the WriteLine() method, you'll see something much more complicated:

The first thing you'll see is the word void - this means that the method does not "return" or "evaluate" to anything. In case you'd been wondering, a "method" is some code you want to run from elsewhere. These are also often called "functions" or "procedures" - but usually in C# they are referred to as methods.

Next is the Console.WriteLine - Console is the name of the class that contains the method. For now, think of a class as a container for methods and data. We'll go over it in more detail later. WriteLine is the name of the method, and the dot between it and the class says you're trying to access something inside the class.

Then we have the parentheses: these surround the parameter list. This method has one argument: first, the name of the type, string and a question mark to show that the value can be a null - an indicator that there is no value, not even an empty string. after that is the name of the parameter, value. This is often useful to explain what the parameter is for, but in this case it's really simple so the name isn't very descriptive.

After this is some text about overloads: this means that there are some number of other methods with the same name but different parameter lists. You can look around at those if you can figure out how, but many of them may be confusing.

On the next line is a description of this particular overload, followed by the exceptions that may occur if the method is called. Exceptions are a kind of error that we can go into later. This method is extremely unlikely to actually throw this exception.

That's probably enough about methods for the moment.

Going North

First, we're going to add some code that allows you to choose to go North. We'll do this using an if statement. First we'll put in a blank newline just to keep everything clean.

First, we have the if keyword. This indicates that you'd like to do something (A) if another thing (B) is true. The parentheses mark the condition (which we're calling B). This is composed of some amount of code that evaluates as a boolean value, or in other words True or False. In this case, we use the == or "is equal to" operator which returns True if the things on each side are equal enough. This is followed by either one line of code you want to run in that case, or more commonly some code surrounded by a scope (defined using curly braces) which allows you to run multiple lines of code. Despite the fact that the main purpose of the curly braces is to allow multiple lines of code to execute if the condition succeeds, most people prefer that you always use them even if you only put a single line of code between them, so that it is very clear where the start and end are.

So what happens now? If you run your code, you will see the same response as before. Now, however, if you press the 'N' key on your keyboard, you will see the message "You went North!" before the loop restarts. You can press any other key, and it won't behave that way. Congratulations, you are now actually programming! Almost anything can be accomplished by combining loops and if statements.

Going South

Time to do it for South now! Here, we'll use an else.

Code in an else only runs if the if it follows has its condition evaluate as false.

Now, we still have a problem: no matter what, the greeting for the first room is shown every time. You could write more loops and ifs to give each choice specific handling, greet the user on entering another room, and give options there. You also know enough to save some data about and read it in different rooms to do different things. But if you only use the tools you've learned so far, you'll soon find that your code gets messy, repetitive, and extremely long. In the next lesson, we'll attempt to tackle a few ways to help with that.