×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Two or more IF commands doesn't work
2

Two or more IF commands doesn't work

Two or more IF commands doesn't work

(OP)
First of all, i would like to see goodday to you because this is my first post and topic at this forum.

When i use two or more IF commands, it uses the last one, i'll give an example. I'm making an simple dartbord by the way:

CODE

0 CLS
05 score = 501
10 INPUT "Please typ a throw in numbers:" throw
20 IF throw = 23 THEN mistake = 1 ELSE mistake = 0
30 IF throw = 29 THEN mistake = 1 ELSE mistake = 0
40 IF throw = 31 THEN mistake = 1 ELSE mistake = 0
50 IF throw > 60 THEN mistake = 1 ELSE mistake = 0
60 IF throw < 0 THEN mistake = 1 ELSE mistake = 0
70 IF mistake = 1 THEN PRINT "This number can't be thrown"
80 IF mistake = 1 THEN GOTO 10
90 total = score - throw
100 score = total
110 PRINT "You still have to throw "; throw; " points"
120 GOTO 10

I hope you guys understand this example and can tell my why all these IF-commands (there are around 10 more in an official dartbord but this is just an example).

Perhaps it's usefull if i say this is my first 'quest' with QBasic.

Another question:
Is there any way to write all these IF commands as something like this:

CODE

IF throw = 23,29,31 THEN mistake = 1 ELSE mistake = 0

RE: Two or more IF commands doesn't work

There's a few things.

1.  You need to put an END IF at the end of each IF block

2.  You can put multiple conditions in one IF statement, like so:


CODE

IF throw = 23 OR throw = 29 OR throw = 31 OR throw > 60 OR throw < 0 THEN
   mistake = 1
ELSE
   mistake = 0
END IF

monkeysnake <.

RE: Two or more IF commands doesn't work

You can use SELECT CASE for multiple select.
You can use block IF (IF | END IF pair) to group several statements.
You do not have to use line numbers unless you want to.
You can use string labels , like "again".

Last thing. Help is your friend. Press F1 in Qbasic.

I still do not know that your code supposed to do, though

CODE

CLS
score = 501
again:
INPUT "Please typ a throw in numbers:"; throw
SELECT CASE throw
CASE IS < 0, 23, 29, 31, IS > 60
   mistake = 1
CASE ELSE
   mistake = 0
END SELECT

IF mistake = 1 THEN
   PRINT "This number can't be thrown"
   GOTO again
END IF

total = score - throw
score = total
PRINT "You still have to throw "; throw; " points"
GOTO again

RE: Two or more IF commands doesn't work

(OP)
Well..
I'm just making a very simple dartboard without the dartboard itself :) .
Just the system behind it in QBasic.

Quote:

You can use string labels , like "again".
It doens't really matter right? It's useful when you want to edit, but then again, if you number high enough (in xxx) you don't have to worry about that problem.
It may be a bit more clarifying..

Quote:

Help is your friend. Press F1 in Qbasic.
I'm aware of this function but i don't always understand the tiny help it gives, you know.

Quote:

ou can use block IF (IF | END IF pair) to group several statements.
Thanks, AND didn't work.

RE: Two or more IF commands doesn't work

One of your problems is your if's cascade. line 20 has an if and sets mistake to 1 or 0...then line 30 sets mistake to 1 or 0... then line 40 sets mistake to 1 or 0

The answer is "42"

RE: Two or more IF commands doesn't work

>>You can use string labels , like "again".
>It doens't really matter right?
 that's up to you. You are the  programmer ;))

>>Help is your friend. Press F1 in Qbasic.
>I'm aware of this function but i don't always understand the tiny help it gives, you know.
 then you can always ask here ;))

>>ou can use block IF (IF | END IF pair) to group several statements.
>Thanks, AND didn't work.

???
what didn't work?

CODE

IF mistake = 1 THEN
   PRINT "This number can't be thrown"
   GOTO again
END IF
single condition, two statements grouped - they run if condition is true

RE: Two or more IF commands doesn't work

(OP)
I meant that if i put it like this, it didn't work:

CODE

IF throw = 23 AND throw = 29
@Franklin:
I used monksnakes way, it works fine. :)

RE: Two or more IF commands doesn't work

you have to put the conditional inside parentheses.

Here is a small page explaining this, I apologize for the incorrect syntax earlier.


http://www.svatopluk.com/qbtutor/tut5.htm

monkeysnake <.

RE: Two or more IF commands doesn't work

You must have mistyped.

IF throw = 23 AND throw = 29

or using parentheses

IF (throw = 23) AND (throw = 29)

is never going to evaluate to TRUE no matter how hard you try.

RE: Two or more IF commands doesn't work

so you should use
IF throw = 23 OR throw = 29
instead

RE: Two or more IF commands doesn't work

Yes, the implication was one would use OR instead.

RE: Two or more IF commands doesn't work

(OP)
Thanks for the website too monksnake, but those parenthese aren't obliged. I mean, it works well without them, though it could be useful in more difficult IF-lines.

I.E. :

CODE

x = 16
y = 3

IF ((x > 5 AND x < 10) OR y = 3) THEN PRINT "Correct"

RE: Two or more IF commands doesn't work

The problem was with the "ELSE mistake = 0". Imagine that throw was equal to 23. It would satisfy the first condition, making mistake equal to 1, but then the subsequent code would set it back to 0. Throw would not equal 29 or 31, meaning those two lines would set mistake to 0, indicating no mistake (while there actually was one). By getting rid of the "ELSE mistake = 0" instead having "mistake = 0" before any of the IF statements, you still assure that if none of them are satisfied, mistake will still equal 0. With this way, however, subsequent IF statements will not undo the setting of mistake to 0.

The code would look like this:  

CODE

0 CLS
05 score = 501
10 INPUT "Please type a throw in numbers:" throw
15 mistake = 0
20 IF throw = 23 THEN mistake = 1
30 IF throw = 29 THEN mistake = 1
40 IF throw = 31 THEN mistake = 1
50 IF throw > 60 THEN mistake = 1
60 IF throw < 0 THEN mistake = 1
70 IF mistake = 1 THEN PRINT "This number can't be thrown"
80 IF mistake = 1 THEN GOTO 10
90 total = score - throw
100 score = total
110 PRINT "You still have to throw "; throw; " points"
120 GOTO 10

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close