Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:47 25 Nov 2024 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 : (MM BASIC 2.7B) ELSEIF

Author Message
pcaffalldavis

Senior Member

Joined: 17/10/2011
Location: United States
Posts: 187
Posted: 01:41am 27 Dec 2011
Copy link to clipboard 
Print this post

Hello all! Hope you all had a Merry Christmas!

I'm using MM version 2.7B and its BASIC.

I’m having trouble writing an ELSIF and could use a working example of a IF THEN ELSE that includes a second internal ELSEIF THEN ELSE. I’ve tried a number of different ways to write this, but can’t seem to get it to work.

I need to print a temperature to screen. If it is single digit I print it one character space to the right so all numbers appear in a column as if they were right registered. If the TEMP is double digit I locate it 13 pixels to the left. I’m using FONT 2 which is 11 pixels wide, but 13 seems to be the correct number to move them left and right for alignment purposes.

The need for a second sub ELSEIF arises because sometimes the TEMP is also the high or low temperature for that day. When the current temp is also equal to HIGHTEMP or LOWTEMP then I want to display it in reverse.

Getting it displayed is easy:

IF TEMP <10 THEN ‘ single digit display is next
LOCATE 400,14: PRINT TEMP ‘ for single digit TEMP
ELSE
LOCATE 377,14: PRINT TEMP ‘ for 2 digit TEMP
ENDIF



Getting it displayed in reverse when appropriate has not been so easy. When I try to put in the second set of IF THEN ELSE or ELSEIF THEN ELSE statements I start having trouble:

IF TEMP <10 THEN
IF TEMP = HIGHTEMP OR LOWTEMP THEN
FONT 2,1,1 ‘ reverse font’
LOCATE 400,14: PRINT TEMP ‘ prints 1 digit TEMP in reverse font
FONT 2 ‘ back to regular font 2
ELSE
LOCATE 400,14: PRINT TEMP ‘ prints 1 digit TEMP in regular font
ELSEIF ‘ TEMP is equal to 10 or greater so it is registered to print one space left
IF TEMP = HIGHTEMP OR LOWTEMP THEN
FONT 2,1,1 ‘reverse font
LOCATE 377,14: PRINT TEMP ‘prints 2 digit TEMP in reverse font
FONT 2, ‘back to regular font 2
ELSE
LOCATE 377,14: PRINT TEMP ‘prints 2 digit TEMP in regular font
ENDIF



What am I doing wrong with this code? Does someone have an example of a working IF THEN ELSE with an internal subset of IF THEN ELSE that uses ELSEIF correctly so I can learn how to do this.

Back in the day I had a nice manual that came with GW Basic and it included working examples of every command. Wish I still had that manual.

Pete in Hyder
We're all here 'cause we're not all there.
 
trippyben

Regular Member

Joined: 26/06/2011
Location: Australia
Posts: 91
Posted: 02:14am 27 Dec 2011
Copy link to clipboard 
Print this post

Don't you need to ENDIF each IF statement?

i.e.

IF xx1 THEN 'IF statement 1
IF xx2 THEN 'IF statement 2
ELSE yy2
ENDIF 'END of IF statement 2
ELSE xx2
IF xx3 THEN 'IF statement 3
ELSE yy3
ENDIF 'END of IF statement 3
ENDIF 'END of IF statement 1


That way each IF statement is terminated within the correct section?

I could be wrong - someone with a bit more experience may be able to verify (or not)Edited by trippyben 2011-12-28
 
ajkw
Senior Member

Joined: 29/06/2011
Location: Australia
Posts: 290
Posted: 02:34am 27 Dec 2011
Copy link to clipboard 
Print this post

Hi,

I think the problem is the IF following the ELSEIF in the first line.

ELSEIF ‘ TEMP is equal to 10 or greater so it is registered to print one space left
IF TEMP = HIGHTEMP OR LOWTEMP THEN

change to

ELSEIF TEMP = HIGHTEMP OR LOWTEMP THEN ‘ TEMP is equal to 10 or greater so it is registered to print one space left


In other words, following the command ELSEIF should be the condition to be compared not another IF command

eg.
IF condition THEN
'stuff to do
ELSEIF condition THEN
'other stuff to do

Cheers,
Anthony.Edited by ajkw 2011-12-28
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6097
Posted: 03:07am 27 Dec 2011
Copy link to clipboard 
Print this post

Pete,
This is a basic use of ELSEIF
It takes the reading from my wind direction which doesn't have a simple correlation with the direction.
The series of ELSEIF's convert the voltage into compass points with w=17 for faulty cable.

1000 rw= reading(2)
1010 IF rw>w1 THEN
1020 w= 17
1030 ELSEIF rw > w2 THEN
1040 w= 12
1050 ELSEIF rw > w3 THEN
1060 w= 14
1070 ELSEIF rw > w4 THEN
1080 w= 13
1090 ELSEIF rw > w5 THEN
1100 w= 16
1110 ELSEIF rw > w6 THEN
1120 w= 15
1130 ELSEIF rw > w7 THEN
1140 w = 10
1150 ELSEIF rw > w8 THEN
1160 w = 11
1170 ELSEIF rw > w9 THEN
1180 w = 2
1190 ELSEIF rw > w10 THEN
1200 w = 1
1210 ELSEIF rw > w11 THEN
1220 w = 8
1230 ELSEIF rw > w12 THEN
1240 w = 9
1250 ELSEIF rw > w13 THEN
1260 w = 6
1270 ELSEIF rw > w14 THEN
1280 w = 7
1290 ELSEIF rw > w15 THEN
1300 w = 4
1310 ELSEIF rw > w16 THEN
1320 w = 3
1330 ELSE
1340 w = 5
1350 ENDIF


W2 to W16 are the voltages between the various directions.
AS soon as a match is found the following block of code is run then the code jumps to after the ENDIF

JimEdited by TassyJim 2011-12-28
VK7JH
MMedit   MMBasic Help
 
Bryan1

Guru

Joined: 22/02/2006
Location: Australia
Posts: 1344
Posted: 03:09am 27 Dec 2011
Copy link to clipboard 
Print this post

One problem with that

'ELSEIF TEMP = HIGHTEMP OR LOWTEMP THEN ‘ TEMP is equal to 10 or greater so it is registered to print one space left' statement is using OR ( as it is a bitwise operator)

Below is the explanation for 'IF' command and it is pretty self explanatory


Multiline IF statement with optional ELSE and ELSEIF cases and ending
with ENDIF. Each component is on a separate line.
Evaluates 'expression' and performs the statement(s) following THEN if the
expression is true or optionally the statement(s) following the ELSE
statement if false.
The ELSEIF statement (if present) is executed if the previous condition is
false and it starts a new IF chain with further ELSE and/or ELSEIF
statements as required.
One ENDIF is used to terminate the multiline IF.

Regards Bryan
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 03:13am 27 Dec 2011
Copy link to clipboard 
Print this post

2 lines of code achieving your stated aim

[code]
10 IF (lowtemp=temp) OR (hightemp=temp) THEN FONT 2,1,1 ELSE FONT 2,1,0
20 LOCATE 400,14 : PRINT FORMAT$(temp,"%4g")
[/code]

Life is too short for Complex Multi-line IF/ELSE/THEN/ENDIF structures


Edited by djuqa 2011-12-28
VK4MU MicroController Units

 
pcaffalldavis

Senior Member

Joined: 17/10/2011
Location: United States
Posts: 187
Posted: 03:15am 27 Dec 2011
Copy link to clipboard 
Print this post

I think I tried Trippybens already, but found that it always printed in reverse, even when the TEMP was not equal to HIGHTEMP or LOWTEMP.

As I look in the MM manual ELSIF only shows a single ENDIF for the entire example.

The problem as I see it is that I need two complete IF THEN ELSEs within the higher IF THEN ELSE, one is after the first IF THEN, and the second is after the highest level ELSE.

The problem seems to be that the highest level ELSE seems to be getting mixed up with the two sublevel ELSEs.

I'm sure there is a way to do this, even to more than two sublevels. It's just that I don't know or remember how.

Pete in Hyder
We're all here 'cause we're not all there.
 
ajkw
Senior Member

Joined: 29/06/2011
Location: Australia
Posts: 290
Posted: 03:21am 27 Dec 2011
Copy link to clipboard 
Print this post

Peter,

Actually, the FORMAT command will do what you want in regards justification.

ALSO

Note the important change in this example to the expression in the IF statement

You cannot have
IF t = x or y
it must be
IF t = x or t = y



FONT 2
IF TEMP = HIGHTEMP OR TEMP = LOWTEMP THEN FONT 2,1,1
LOCATE 3xx,14 'work out xx as required
PRINT FORMAT$(TEMP,"%4.1F") ' FORMAT TO 4 CHAR'S WITH A PRECISION OF 1
FONT 2



Cheers,
Anthony.
 
pcaffalldavis

Senior Member

Joined: 17/10/2011
Location: United States
Posts: 187
Posted: 03:26am 27 Dec 2011
Copy link to clipboard 
Print this post

djuqa,

That is sweet!

I'm still going to wonder if it is possible to have full sets of IF THEN ELSEs within a higher level IF THEN ELSE (one after the high level THEN and one after the high level ELSE.

I'm sure this was possible in GW BASIC, but I can't remember the structure.

Thank you all though. I see now how ELSEIF is used after an IF THEN. That is helpful TassyJim.

Pete in Hyder
We're all here 'cause we're not all there.
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 03:39am 27 Dec 2011
Copy link to clipboard 
Print this post

  pcaffalldavis said   djuqa,
That is sweet!

Thanks , I will post shortly an example of multi-level and multi-line if/then/else/maybe/couldbe/endif structures for the benefit of the masochists that think somehow that the harder to read a program the better it is.

Software engineering principles should be applied to all programming tasks.
If it appears to hard to understand then maybe it is in need of a major re-think.

In 30 years of programming commercial and Industrial applications, I have never needed a MULTI-level IF/THEN/ELSE structure.


Edited by djuqa 2011-12-28
VK4MU MicroController Units

 
pcaffalldavis

Senior Member

Joined: 17/10/2011
Location: United States
Posts: 187
Posted: 04:28am 27 Dec 2011
Copy link to clipboard 
Print this post

djuqa,

I agree with you completely. It is just that I am completely self-taught, with most of my coding having been done and learned years before the web and forums were all that helpful or available.

If I were learning today (for the first time) I'm sure all these helpful answers I'm learning here would make me a better coder.

The last time I had to code in GW BASIC was in 1989, and that program is still running today. Still pays for my groceries amazingly enough. It has too many multi-lever IF THEN ELSES, but it was all I knew at the time. I did learn Foxpro and write a few programs with it in 91, but that is the last time I programmed anything more complex than a digtal watch or DVR.

Appreciate your help, all of you! If any of you are able to teach me how to code for situations where there seem to be multi-layers of logical IF THEN ELSEs I try to remain teachable!

Cordially,

Pete in Hyder


We're all here 'cause we're not all there.
 
Print this page


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

© JAQ Software 2024