Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:17 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Compare Bugs ?

Author Message
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 07:24am 21 Dec 2021
Copy link to clipboard 
Print this post

I try to check if a number is ODD or EVEN using PICOMITE 5.07.01

dim integer I,P
dim float F

for P=0 to 15

I=P/2
F=P/2

if I=F then print "ODD"
else
print "EVEN"
endif

next

I get always ODD !
What is wrong ?

Thanks for your help

Georges
Edited 2021-12-21 17:35 by georgestheking
 
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 07:45am 21 Dec 2021
Copy link to clipboard 
Print this post

After a lot of testing I found that this version is working  

dim integer I,P
dim float F

for P=0 to 15

I=P/2
F=P/2

if I=F then
print "ODD"
else
print "EVEN"
endif

next
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2640
Posted: 07:58am 21 Dec 2021
Copy link to clipboard 
Print this post

An observation.

On an F4 I get this:-
> for p%=0 to 15 :i%=p%/2 : f=p%/2 : if f=i% then : print "odd" : else : ? "even" :endif : next
odd
even
odd
even
odd
even
odd
even
odd
even
odd
even
odd
even
odd
even
>
But on a Pico this:-
>  for p%=0 to 15 :i%=p%/2 : f=p%/2 : if f=i% then : print "odd" : else : ? "even" :endif : next

Error : Cannot find a matching FOR
>

Same sort of error if I try a Do...Loop Until - no matching Do

Pico not quite the same as the rest in Immediate Mode. No loops it seems.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 08:00am 21 Dec 2021
Copy link to clipboard 
Print this post

The close of the loop is only being executed as part of the If. Strange that it is not being picked up in both versions.

as to the original inquiry; for integers you can use a bitwise AND (very fast, and a bit faster still if you specify the radix):

...
If (P And &h1) Then 'odd
...


for (whole number) floats you can say

...
If (P/2)<>Int(p/2) Then 'odd
...

which is pretty much what you were doing anyway but avoids mixing datatypes (here be dragons).
Edited 2021-12-21 18:38 by CaptainBoing
 
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 08:03am 21 Dec 2021
Copy link to clipboard 
Print this post

Nice Tricks Captain,

Thanks,
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 08:03am 21 Dec 2021
Copy link to clipboard 
Print this post

That's perfectly correct.

IF test THEN process

is an abbreviation that can be on a single line only because there are no alternatives.

Unabbreviated it would be

IF test THEN
 process if passed
END IF

the correct form of the command with an alternative is

IF test THEN
 process if passed
ELSE
 process if failed
END IF
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
wolfme
Newbie

Joined: 26/10/2021
Location: Germany
Posts: 31
Posted: 08:56am 21 Dec 2021
Copy link to clipboard 
Print this post

Another way (and in one line):

For r%=0 To 15 : If r% Mod 2 Then Print "odd" Else Print "even" : Next r%


your code from above should be like this on pico:

For p%=0 To 15 :i%=p%/2 : f=p%/2 : If f=i% Then Print "odd" Else Print "even" : Next


EDIT: it seems that your code from above ist wrong, it prints the opposite (1 is even, 2 is odd and so on)
Edited 2021-12-21 19:08 by wolfme
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2640
Posted: 12:42pm 21 Dec 2021
Copy link to clipboard 
Print this post

"Pico not quite the same as the rest in Immediate Mode. No loops it seems."
Found the problem.
Line length too long, despite all characters showing on the screen some were being lost. Using OPTION DISPLAY 50,120 fixed it.
 
wolfme
Newbie

Joined: 26/10/2021
Location: Germany
Posts: 31
Posted: 01:36pm 21 Dec 2021
Copy link to clipboard 
Print this post

aaah, ok. You typed in to command line.
I always use option display 50,132 thats maximum i think.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 02:01pm 21 Dec 2021
Copy link to clipboard 
Print this post

  phil99 said  "Pico not quite the same as the rest in Immediate Mode. No loops it seems."
Found the problem.
Line length too long, despite all characters showing on the screen some were being lost. Using OPTION DISPLAY 50,120 fixed it.

This is interesting. I've encountered this error, but never figured out the reason, so thank you.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 08:21pm 21 Dec 2021
Copy link to clipboard 
Print this post

>
 dim integer P

 for P=0 to 15
   if p mod 2 then
     print p,"ODD"
   else
     print p ,"EVEN"
   endif
   
 next p
 


RUN
0   EVEN
1   ODD
2   EVEN
3   ODD
4   EVEN
5   ODD
6   EVEN
7   ODD
8   EVEN
9   ODD
10    EVEN
11    ODD
12    EVEN
13    ODD
14    EVEN
15    ODD
>


Jim
VK7JH
MMedit
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025