Lesson 2  Logic

Yes, I lied.  We're not doing functions yet.  I got carried away.  we're going to do logic this lesson.  This is where you figure out what's going on with a lot of stuff.  An overview of commands that deal with logic:

if()
for()
while()
else

  If.  This function does just what it says.  It says "if <blah> then...".  An example:

if (Nightshade == cool)
  write("wow, Nightshade is cool!");

A few things to note:  'Nightshade' and 'cool' are variables of some type.  If you want to see if a string equals some text, be sure the text has "s around it.

string str;

str = "blah";

if (str == "blah")
  write("This is true!");

You may have noticed that in the if statment, I put 2 = signs.  why?  because one = is used to assign a value.  not to see if 2 things are equal.  Questions?

Why is write() indented?  is that nessecary?

write is indented to help understand what the if is executing.  when you get into more complicated if statments, it will really come in handy organization-wise.  but otherwise, the indent is not neccesary.

What if I want a bunch of stuff to happen if str == "blah"?

That's when we pull out the {}s.  If you put {} after the if statment, everything inside of the {} will be run.  for example:

int shade;

shade = 10;

if (shade == 10) {
  write("The shade variable is 10!");
  write("well, duh!");
}

If shade does not equal 10, both of the write commands will be skipped.  Some other comparisons you can make are:

>   is greater than (being equal isn't included)
<   is less than (ditto)

One thing you can also do is check to see if the variable has a value.  this is how:

int night;

if (night)
  write("blah");

the variable night exists, but has no value.  this if statment says "if night has a value, then"  OR you can do this:

int night;

if (!night)
  write("blah");

this if statment says "if night DOESNT have a value, then".  If you haven't noticed, ! means 'not' in code.  Now let's throw in else.

int bob;

bob = 1;

if (bob == 0)
  write("bob doesn't equal 0 you fool!");
else
  write("bob does equal one, though.");

else pretty much says "if (bob == 0) ends up being false, then do this:"  you can also add {} after the else to execute multiple commands.

for() and while are a little more complicated (I bet you love hearing that :P) here's how for() goes:

for (give a variable a value; if statment goes here; a command to execute every round) 
  {commands}

That probably seems more complicated than it is.  the for statment executes the commands until the if statment is false. (note: a variable with ++ after it adds one.  so if blah = 1, then blah++ equals 2)

int x;

for(x = 0; x == 10; x++) {
  write("Spam alert!!");
  }

write("x now equals 10");

Here's a rundown of what that code does.  First, we declared a variable x.  then, in the for() statment, we made x = 0.  the commands go through, then the x++ part of for() gets run.  Now x = 1.  For goes through the process again. It will keep doing this over and over until x == 10 (the middle part).  So, if this ran, you'd get "Spam alert!" ten times.  Much easier and easier on the server than typing write("Spam alert!"); ten times.  When we do arrays, you'll just how useful the for() command really is.  Now for the while() command:

int x;

x = 2;

while (x < 4) {
  write("blah");
  x++;
  }

There, we made x = 2, and the while statment checked if x was less than 4.  x is 2, so it executes the command, writing "blah, and adding 1 to x.  x now equals 3.  x is still less than 4, so it runs the command again, adding one more to x, making it 4.  x is now equal to 4, and not less than 4.  so the while() statment stops.  as a result you get "blah" written twice.

A note about "true" and "false".  you can designate true and false with numbers, too.  if you put something like if(0), that would be false.  anything above 0 would be true.

