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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value comparison problem 4

Status
Not open for further replies.

foxuser20

Programmer
Jan 7, 2016
7
PH

Code:
if val(tor_table.grade) > 3 && where grade field is a character so i put the val() to convert it
messagebox("grade failed")
else
messagebox("grade pass")
endif

and when the value of grade is 1, the popup message is the 1st messagebox.
am i doing wrong on the code, if so pls help
 
As far as I can see, the code is OK. If the value really is "1", you should see the second message ("grade pass"). Are you sure about the value of the field? And are you sure you are looking at the correct record?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Put a breakpoint there and then hover the mouse over the tor_table.grade and let it show you what the value is.

Val("1") is 1 for sure. Same for Val("1 ") and VAL(" 1")

Bye, Olaf.
 
actually this is my code:


Code:
LOCATE FOR id_no = thisform.txtId_no.Value  AND tor_table.scode = ALLTRIM(auto_table.pre1) OR tor_table.scode = ALLTRIM(auto_table.pre2) AND VAL(tor_table.grade) < 3 
IF FOUND()
	MESSAGEBOX("Pass")
	ELSE 
	MESSAGEBOX("fail")
	ENDIF

is there wrong in my conditions?
 
Check what condition is failing. We can't tell you what's right or wrong, but you certainly have to have more than just a grade lower than 3 to pass.
You might want to put paranthesis around some of the conditions.

In short you have the condition [tt]a AND b OR c AND d[/tt], which is true, if [tt]a AND b[/tt] are both true or if [tt]c AND d[/tt] are both true.

Bye, Olaf.


 
Again said, set a breakpoint and see at the data. Yes, the record FOUND() can have a grade >= "3", if id_no matches and scode matches pre1, the third and fourth condition then don't matter anymore.

Like in math multiplication and division has precedence over addition and subtraction (you might know a rule named PEMDAS) the same goes for boolean operators AND and OR.

Bye, Olaf.
 
You should definitely use parentheses in the condition. It's very easy to get the precedence of AND and OR wrong.

Some other things to check:

- If scode or pre1 or pre2 might contain alphabetic characters, it might be necessary to wrap them in UPPER() (to avoid case-sensitivity issues).

- If thisform.txtId_no.Value is a character field, you should put ALLTRIM() round it.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
i got it working but without this code...
Code:
 OR tor_table.scode = ALLTRIM(auto_table.pre2)

any other options plz?
 
Paranthesis. You should be able to figure it out yourself.

I'll give you another hint:

Code:
? .t. AND .t. OR .f. AND .f.

This is .T., though the last .f. is applied with AND. This last .f. stands for a grade not being smaller than 3, so this can be false and still the whole expression is true.

Bye, Olaf.
 
Like others, I believe this is a matter of order of precedence.
"And" is evaluated before "OR", like "*" before "+"

1 + 1 * 0 = 1 + (1 * 0) = 1 + 0 = 1
while
(1 + 1) * 0 = 1 * 0 = 0

similarly

.t. or .t. and .f. = .t. or (.t. and .f.) = .t. or .f. = .t.
while
(.t. or .t.) and .f. = .t. and .f. = .f.

Order of precedence for logical Operators


Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Indeed, and if that still is not resolved, think abouit the two conditions about scode. It's correct to OR them, but you want this to happen first (OR operations done before AND operations), while this operation is done last. Put brackets around that:

[tt]a AND (b OR c) AND d[/tt] forces the correct order of operations for your conditions. Without the brackets there the execution is in reverse order, meaning (a AND b) OR (c AND d) is executed, which can be true, even if d is false, in the case I showed, where a and b are true, thus c and d don't matter anymore.

You only have three real conditions:

1. id_no matches a certain value
2. scode matches one of two values
3. grade is ok

You want to combine these three with AND, you need to OR the two partial conditions of 2 (because no code can match two other codes, only one of them) and therefore need put brackets around the whole second condition.

Bye, Olaf.
 
"And" is evaluated before "OR"

In all my years as a programmer, I don't recall ever trying to memorise that. I always use parentheses to force the precedence, whether they are strictly necessary or not. Not only does that remove any risk of error on my part, but it also makes my intentions obvious to anyone else who has to work with my code.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I agree with
I always use parentheses to force the precedence, whether they are strictly necessary or not.

But from the truth table for "AND" and "OR", where "false" is encoded with "0" while "true" is encoded with "1", I remember the correspondence between AND and multiplication, respectively between OR and summation (with a single exception).

Despite of that, using parentheses
makes my intentions obvious to anyone else who has to work with my code

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania

 
I also use a generator for WHERE clauses and it always puts brackets around the partial conditions it ANDs, no matter whether they are necessary or not. There also is no postprocessing for eliminating unneccessary brackets.

Bye, Olaf.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top