Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:14 23 Jul 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 : elseif statement

Author Message
Chrisk

Senior Member

Joined: 21/12/2014
Location: Australia
Posts: 122
Posted: 11:46pm 16 Aug 2015
Copy link to clipboard 
Print this post

Hi Guys

I have 2 temperature sensors and I wish to carry out different subroutines based on their values.
The following example gives Error: No closing bracket in expression.
Obviously the syntax is not correct for MMBasic 4.5
I also have a need to go to a sub if T1 or T2 are over certain values.
I have managed the later but it is very slopy.


TRON
T1 = 25
T2 = 30
If T1 > T2 then
gosub greater
elseif ( T1 > 18 && T1 < 28 ) then
gosub abcd
else
endif
end
greater: 'test1 OK
end
abcd: 'test2 OK
end

Any suggestions would be greatly appreciated.

Chrisk
 
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
Posted: 11:57pm 16 Aug 2015
Copy link to clipboard 
Print this post

The following is untested as I don't have a uM to hand, but it is how I would do it.

[code]
TRON

T1 = 25
T2 = 30

If T1 > T2 then greater
if ( T1 > 18 && T1 < 28 ) then abcd

end

sub greater
'test1 OK
end sub

sub abcd
'test2 OK
end sub
[/code]

Cheers
Chris

EDIT:
Just realised in the above that is both conditions are true it will run both subs.
If that is not acceptable then try:
[code]
TRON

T1 = 25
T2 = 30

If T1 > T2 then
greater
else
if ( T1 > 18 && T1 < 28 ) then
abcd
end if
end if

end

sub greater
'test1 OK
end sub

sub abcd
'test2 OK
end sub
[/code]
Edited by Chris Roper 2015-08-18
http://caroper.blogspot.com/
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1580
Posted: 12:15am 17 Aug 2015
Copy link to clipboard 
Print this post

@Chrisk

elseif ( T1 > 18 && T1 < 28 ) then


what do the "&&" mean? Is that MMBasic?

Regards
Michael

Edit:
as Chris Roper mentioned:

[code]T1 = 25
T2 = 30

If T1 > T2 then
greater
end
else if ( T1 > 18 and T1 < 28 ) then
abcd
end
else
Print "oops"
end if

end

sub greater
Print "test1 OK
end sub

sub abcd
Print "test2 OK "
end sub [/code]

Note: Using "GOSUB" is old style and for compatibility only (IMHO).
Edited by twofingers 2015-08-18
causality ≠ correlation ≠ coincidence
 
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
Posted: 12:20am 17 Aug 2015
Copy link to clipboard 
Print this post

&& is a Logical Operator, it will evaluate the first condition:

T1 > 18

then the second Condition:

T1 < 28

then if Both Conditions are TRUE it will return TRUE as the result, otherwise it returns FALSE and the subroutine call will be ignored.

the longhand way of doing it would be:
[code]
if T1 > 18 then
if T1 < 28 then
'do something
end if
end if
[/code]

Hope that helps

Cheers
Chris

[EDIT]

Replace && with the word AND
I am currently chopping and changing between 3 dialects of BASIC and 2 of C so keep forgetting which one uses which syntax

I am not so sure that MM Basic has a && operator but it does have AND
They are the basically same thing, just syntax difference between languages.
(there are differences but they are subtle and irrelevant in this case, so I won't try to confuse you with an explanation)

EDIT 2: @twofingers, just realised you were asking ChrisK, I read it as Chris but I may as well let the post stand.

Edited by Chris Roper 2015-08-18
http://caroper.blogspot.com/
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1580
Posted: 12:36am 17 Aug 2015
Copy link to clipboard 
Print this post

Please read the edit!


edit:
  Quote  EDIT 2: @twofingers, just realised you were asking ChrisK, I read it as Chris but I may as well let the post stand.




Edit2:
else if ( T1 > 18 and T1 < 28 ) then

for MMBasic <=4.5 the "Else If" must be written as "ElseIf"!

Edited by twofingers 2015-08-18
causality ≠ correlation ≠ coincidence
 
Chrisk

Senior Member

Joined: 21/12/2014
Location: Australia
Posts: 122
Posted: 03:18pm 17 Aug 2015
Copy link to clipboard 
Print this post

Thanks Guys for the prompt replies.
Just to confirm (tested)that MMBasic does not accept the && operator but the AND in its place does work.

Cheers
ChrisK
 
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