Let's make that last program a little smarter.
I want to be able to identify intruders playing
with my programs. Wouldn't it be great if the
computer could recognize someone's name and
print a special message for them? How about
this:
CLS INPUT "Enter your name: ", Name$ IF Name$="Mike" THEN PRINT "Go Away!" ELSE PRINT "Hello, "; Name$; ". How are you today?" END IF
You can change the Name$="Mike" to Name$="Joe",
or whoever you want to go away (like a brother or
sister). Run the program
and type in your name (hopefully it isn't Mike).
You should see the same old message as before.
Now run it again and try entering "Mike" (or "Joe"
or whatever you changed it to).
"Mike" Is Not The Same As "mike"
If it didn't tell the right person to go away,
make sure you typed the name correctly.
In QBASIC,
"Mike" is not the same as "mike", so if you don't
type the first letter in upper-case, the program
won't work. Make sure you enter the name
exactly the way you put it in the program.
IF...THEN
The "IF..THEN..ELSE..END IF" statement in this program
checks to see if Name$ has "Mike" in it. If
so, then it does the PRINT statement after the
"THEN". If Name$ isn't "Mike", it does the PRINT
statement after the "ELSE". "END IF" tells QBASIC that
the "IF" is over.
Conditions
The Name$="Mike" portion of the IF...THEN is
called the "condition". With numbers you can
also check for "greater than" and "less than":
CLS INPUT "Enter a number: ", Number IF Number < 100 THEN PRINT "Your number was less than 100" ELSE PRINT "Your number was greater than or equal to 100" END IF
If you want to check for "greater than", use "Number > 100".
Equals works just like before, "Number = 100". Another option is
"not equal" which can be done like this: "Number <> 100".
IF...THEN is one of the most powerful features of QBASIC.
Using IF...THEN can make your programs very interesting.
It lets your program make decisions and do something
appropriate.
Learned
Source: http://jpsor.ucoz.com |