October 30, 2025, 07:11:48 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Comparing time.

Started by Egil, April 22, 2008, 03:06:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil

April 22, 2008, 03:06:20 PM Last Edit: April 22, 2008, 04:40:37 PM by Egil
To learn using EB, I decided to make a utility that reads loggfiles made by  radio decoding software  running on one PC, extract and sort the needed information, and pass it on to map plotting software running on another PC via my wireless LAN. The communication is made by UDP.
I now have an early console version up and going, and everything seems to work. The UDP routines were the easiest part to produce, thanks to nice examples given on the forum by Sapero andpistol350.

But when trying to make simple routines to sort records by time, I ran into a strange problem.
To make such a sorting routine, I decided to try out the most obvious thing first, e.g. comparing two log times defined as strings ( the same format as the TIME$ function), using the logical operators > and <.

But the code IF "18:32:48" > "22:58:11" THEN PRINT "False" ELSE PRINT"True" gives an opposite opposite result than what I expected. Can anyone explain why?





Support Amateur Radio  -  Have a ham  for dinner!

pistol350

Hi Egil!

that works for me :


STRING currentTime, refTime
currentTime = TIME$
reftime = "22:58:11"

OPENCONSOLE
PRINT currentTime
IF currentTime > refTime THEN PRINT "False" ELSE PRINT"True"

PRINT "press a key to close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END
Regards,

Peter B.

Egil

April 22, 2008, 04:24:01 PM #2 Last Edit: April 22, 2008, 04:30:43 PM by Egil
That is exactly what I mean.

If you put the value reftime = "10:00:00" and TIME$ returns the value "23:15:00", you get the answer True.
Shouldn't it be the other way round?
Maybe I better go to sleep here, it's now 30 minutes past midnight here. I'll try again tomorrow. ;D
Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

you've got your "true" and "false" backwards

IF currentTime > refTime THEN PRINT "False" ELSE PRINT"True"should be
IF currentTime > refTime THEN PRINT "True" ELSE PRINT"False"

If-then- else statements work like this

if condition is true then do true
if condition is false then don't do true else do false


Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

pistol350

Larry Got it right!

IF "18:32:48" > "22:58:11" THEN PRINT "False" ELSE PRINT"True"

I think that i misunderstood Egil's intentions at first.
in that statement, i now guess that "18:32:48" refers to "refTime" while "22:58:11" refers to "currentTime".
And indeed, as Larry said, "you've got your "true" and "false" backwards".

One does so many mistakes at night  ;D
Regards,

Peter B.

Egil

Well, I'm an early bird, so guess I was a bit tired last night. But I still don't get it. If I ask if 1 is greater than 2, the correct answer is NO. Try this:   IF 1 > 2 THEN PRINT "False" ELSE PRINT "True"
In human language this should read "Is ONE greater than TWO?" The computer says "True" e.g. YES. And that is a lie!  ;)

Guess I'll just have to accept it.
Support Amateur Radio  -  Have a ham  for dinner!

GWS

No .. no .. don't accept it Egil ..  :)

It's just a confusion of 'algebra' over 'logic' ..  :o

Obviously (1 > 2) is FALSE, and if you run this little program, you'll see x evaluates to '0' - ie .. False.

openconsole
def x:int
cls

x = (1>2)
print x

IF (x = 1) THEN PRINT "True" ELSE PRINT "False"

do:until inkey$<>""
closeconsole
end


You need to do a test in the 'If' statement for 'True' - and True = 1  :)

So when you write:

if 1>2 then print"False" else print"True"

you get (1>2) set to '0' (ie False), and the if statement goes to the print"True" part.

The logic test would have to result in True (ie 1) for it to go to the "True" part ..

all the best, :)

Graham

Tomorrow may be too late ..

LarryMc

April 23, 2008, 06:24:25 AM #7 Last Edit: April 23, 2008, 06:29:07 AM by Larry McCaughn
QuoteIF 1 > 2 THEN PRINT "False" ELSE PRINT "True"
In human language this should read "Is ONE greater than TWO?" The computer says "True" e.g. YES. And that is a lie! 
Egil

It's not the computer that is actually lying.  YOU have programmed it to lie to you.

Another way to write is your if statement above is:
if 1> 2
  print "false"
else
  print "true"
endif

A generic way to write that is:
if condition
  'do desired action for a true condition
else
  'do desired action for a false condition
endif

Merging the generic with your code:
if 1>2   ' the condition is not true; therefore it is false
  'do desired action for a true condition
  print "false"
else
  'do desired action for a false condition
  print "true"
endif


Which means when the condition is false YOU are telling it to print "true".

The compiler is evaluating the if statement correctly and doing exactly what you are telling it to do for a false condition.

That's why I had said your "true" and "false" were swapped.

From EBasics Help file:
QuoteIF Statement

The IF statement will probably be your most used conditional statement. The statement has two forms, the block IF and a single line IF. The block form executes on or more statements if a condition is TRUE


IF condition {THEN}
'Statement(s) to execute if condition is TRUE
ELSE
'Statement(s) to execute if condition is FALSE
ENDIF

The condition is any math, comparison or algebraic expression that results in a yes or no result.  The ELSE keyword is optional and represents the group of statements that will be executed if the condition is false. The THEN keyword in a block IF is also optional and is supported for backwards compatibility with other languages. Every block style IF must be matched with a corresponding ENDIF statement


Also suggest you look at and run the examples in that same section of the help file.

Hope this helps you understand.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Egil

Guess my brain has become a little rusty...  :-[
Now I suddenly see what you all have been trying to tell me. This was very important for me to understand, because when (or if...!) I get more confident with windows programming, and if I'm able to get a desription of the modulation methods, I plan to "roll my own" decoding software for digital transmissions. And 90% of such decoding routines wil most likely consist og simple test, like the one I have been struggling with the last two days.

Thanks guys ,for bearing over with me. I have just started programming again after approximately twenty years doing other things.
And now I have rediscovered what great FUN this really is.  :)
Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

Quote from: Egil on April 23, 2008, 06:59:32 AM
... I plan to "roll my own" decoding software for digital transmissions. And 90% of such decoding routines wil most likely consist og simple test,...

You might want to study the Select - Case - EndSelect statement in detail.
Where you have a variable that can take on numerous values and you want to do something different in response to each value a SELECT is much better than a bunch of IF statements.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

GWS

Having '0' for 'False' and '1' for 'True' can be a bit confusing.

Writing :  if (x = 1) then print "True", isn't as meaningful as:

if (x = True) then Print "True".

EBasic has the logic constants "True" and "False" built in.

For CBasic, you can use:


def True, False : Int

False = (1 = 2)
True = NOT False


This results in False = 0, and True = 1, which is as required.

You can then write:

if (x = True) then Print "True"

and all is well ..  :)

best wishes,

Graham
Tomorrow may be too late ..