Friday, February 24, 2012

The If Sentence

Source

If you have done any programming whatsoever, you should have the basic knowledge of the if sentence, possibly the single most important command that a computer possesses.  If you don’t know it, fear not, for I am here to enlighten you.

First, some theory.  The basis of AI is essentially if sentences (okay, it’s not nearly that simple, but it’s an important building block).  If this situation happens, react in this manner.  Therefore, knowing the if sentence will enable basic AI.

The basic syntax for an if sentence is : if <the condition> = <the result> then <command>

Now, an example.  Say you’re writing in Quick Basic (the only language I know deeply).  You are told to write a program in which a name and password is checked, a basic version would be this (everything after //s is comments by me and would not be read by the program):

Print “Please input your name and then your password:” //this puts the text in the quotes on the screen
input name$  //This is simply a command that prompts the user to type something and then records the letters in the variable name$
input password$ //same as above
if name$ = “Jake” and password$ = “ender” then print “You gave the correct name and password!”  // this checks whether the variable name$ contains the text “Jake” and if at the same time password$ contains “ender”.  When both these are true, the command print “…” is performed.

And that’s that!

There are more advanced things like “else” and nested ifs, but that’s for later.

No comments:

Post a Comment