Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two or more IF commands doesn't work 2

Status
Not open for further replies.

Dronefang

Programmer
May 23, 2007
4
NL
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
 
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


[monkey][snake] <.
 
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
 
Well..
I'm just making a very simple dartboard without the dartboard itself :) .
Just the system behind it in QBasic.
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..

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.

ou can use block IF (IF | END IF pair) to group several statements.
Thanks, AND didn'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"
 
>>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
 
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. :)
 
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.

 
so you should use
IF throw = 23 OR throw = 29
instead
 
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"
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top