Part II: Variables, Math, and Logic, Oh My!
Alright then, now we get down to the nitty-gritty and start working with stuff that'll look more like a "real" program. Well, kinda... we're
still working with console applications for now, but hopefully we'll get past that in a couple more lessons and move into working with Windows
"Forms" applications. But I digress...
If you thought your high-school algebra was useless (and believe me, I certainly did), I hate to break it to you, but it's going to come back to bite
you in the rear now. Remember sitting there, thinking "What the hell? When did math have letters?" Well, those letters -- the infamous 'x' --
are basically what we're dealing with now: variables.
Just to make sure we're on the same page, a variable is something that can change. It varies, it's not constant. And variables play a humungous
role in programming -- in fact, just about everything you do in coding deals with a variable of one kind or another. So, without further ado, why
don't you go ahead and start up a new Console Application in Visual Studio -- name it whatever you like; "Variables" would probably be okay. Find
your "main" method, and start a new line like you did last time. Add the following lines:
int myNum = 42;
Console.WriteLine("My number is " + myNum);
Build and run it -- remember to add Console.ReadLine() call if the window closes before you can admire your handiwork. You should have had an output
like this:
My number is 42
If not, let me know and we can figure out what went wrong. Anyways...
"myNum" here is the variable -- even though it didn't change. "int" is the data type of the variable, and we'll cover those a bit more in a
second here; basically though, you're telling the compiler "I want a number (an integer, or a number without decimal places) and I'm going to refer
to it as 'myNum' whether you like it or not." From that point on, your program has a place in its memory set aside for an integer, and whenever
you refer to "myNum", it knows to go to that spot in memory and either get the value or change the value, depending on what's going on. We did
both of those in this -- the first line sets the value in memory to 42, and the second line gets the value of myNum from memory.
C# has a few data types that are known in coder-speak as "primitives". This means they're as basic as you can get -- don't worry about the
details of that right now; it'll make more sense later on in the game. "int" is one of these primitives; as I said, it refers to a number that
doesn't have decimal places -- 1, 2, 10, 42, 100, 5678. 12.34 is not an int, because it has a decimal place. Here's a quick list of the primitives
you'll probably run across the most:
* int: a number without decimal places.
* float: a number with decimal places.
* char: a letter (i.e. "A", "b", "c", etc.).
* string: a series of chars -- basically a word, or a sentence, or a paragraph, whatever.
* bool: either "true" or "false" -- this is used for logical stuff, as we'll see later on.
There's a few others, but they're mainly just variations on these, more specifically variations on int and float, and they're mainly concerned with
the size of the numbers you can deal with. We're not worried about that right now.
And, for what it's worth, you've already been dealing with strings; pretty much every time you write something inside of double quotes, like "My
name is Bob", you're creating a string. Strings play a huge part in programming; in fact, they play such a large part that I'm not going to cover
them directly -- we'll use them as we need to, and I'll cover the different aspects of them as we come across them. I know that may seem
counter-intuitive, but just think of it as learning a new language by watching TV shows. (Or think of it as me being lazy; kinda six to one, half a
dozen to the other.)
Math Made Fun (well ... Kinda....)
Yeah, unless you're already in love with math, I don't think there's a whole lot that can be done to make it more exciting. But, luckily, with a
bit of forethought and your trusty C# compiler, you can make math much easier. C# supports all major math operations, either directly or through code
libraries you can use to handle more advanced calculations. Right now, we're worried about the primary operations, and they're pretty much as
you'd expect them to be:
* addition: +
* subtraction: -
* division: /
* multiplication: *
One thing that's a bit different from what you might assume at first, is the equals sign ('='). In programming speak, it's called the assignment
operator, because you assign the value on the right-hand side of the equals sign to the variable on the left hand side, like we did above with myNum.
If you want to, go back and change your code above to read something like this:
int myNum = 42 + 3;
When you run it, it should print out the number '45'. Cool, huh? Feel free to play with it, using the different operators. It's okay, I'll
wait.
Back? Okay, great.
In C#, the standard order of operations apply -- you remember, from algebra, something like "Please Excuse My Dear Aunt Sally", right? You probably
had a different variation, but it means the same thing: Parenthesis, Exponent, Multiplication, Division, Addition, Subtraction. Yes, you can use
parenthesis in your 'equations'. In fact, it's highly recommended, as it generally makes your code much easier to understand. So, take a guess at
what the following will get you -- no fair running it this time.
int myNum = (42 + 3) / 5 - 9;
We'll rewrite it a few different ways here, just to make sure you got it alright:
1) int myNum = ((42 + 3) / 5) - 9;
-- Here, we evaluate the innermost parenthesis -- 42 + 3 -- first, then move out a level and divide by two, then subtract 9. In code speak, that
looks like this:
int myNum = (45 / 5) - 9;
Which becomes:
int myNum = 9 - 9;
Which (as I'm sure you can guess), equals 0.
Now, all this is fine and dandy, but here's one of the cool things: you can use other variables in your equations too. Here's a really simple
example, and then we'll move on to logic -- feel free to ask questions if you need more input on this though.
int myNum = 22;
int myOtherNum = 76;
int mySum = myNum + myOtherNum;
Console.WriteLine("My sum is " + mySum);
Hopefully, you got 98 as your answer; otherwise, one of us did something wrong here.
Anyways, let's move on to logic, shall we?
(Insert Your Favorite Spock Quote Here)
Pretty much everything you'll ever do in programming involves dealing with logic in one form or another. It's just the nature of the beast;
everything is, at some level or another, either on or off, true or false, 1 or 0, etc. This is where the bool data type comes in to play -- as you
remember, it only has two values, either true or false. Don't let the simplicity fool you; it's probably the most important -- or at least the most
powerful -- data type in programming.
Go back to your main method -- or start a new project, if you like -- and put the following code in there (you can add it to the original, or replace
it; doesn't really matter):
bool myBool = true;
if (myBool) [
Console.WriteLine("It was true!");
] else [
Console.WriteLine("It was false...");
]
Go ahead and run it -- it should print out "It was true!". Then go back and change the value of myBool to false and run it again; you should get
the other statement.
This is what is called a "conditional", or an if-else block. And yes, this is an overly simple example; you can do lots of other things inside the
parenthesis where we placed myBool as well. First though, let's look at the different boolean operators we can play with:
* == Tests for equality -- evaluates to true if two values are the same.
* != Test for in-equality -- evaluates to true if two values are not equal.
* > Greater-than -- evaluates to true if the value on the left hand side is larger than the value on the right hand side.
* < Less-than -- evaluates to true if the value on the left hand side is smaller than the value on the right hand side.
* >= Greater-than or equal -- evaluates to true if the value on the left hand side is larger or the same as the value on the right hand side.
* 36) [
Console.WriteLine("45 is bigger than 36");
] else [
Console.WriteLine("It isn't bigger!!!");
]
if (true && false) [
Console.WriteLine("This is a very zen moment...");
] else [
Console.WriteLine("All is right with the universe.");
]
if (!true) [
Console.WriteLine("Not true; that's wierd...");
] else [
Console.WriteLine("Not true must be false....");
]
You can combine different logical operations, like this:
if (true && (42 > 24)) [
Console.WriteLine("It all makes sense now.");
] else [
Console.WriteLine("I'm confused.");
]
Change those around a bit and see what happens. I'm going to let you have at it for a while now; like I said, make sure you come back if you have
any questions. Happy coding