IF
IF
I am working on my programing language and it would help if I'd know how to implement IF-END IF in it. So are there any examples that someone is willing to show?
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
IF you are unfamiliar with IF...END IF, you should not be creating another programming language. You should USE another programming language!
LOL
![Rolling Eyes :roll:](./images/smilies/icon_rolleyes.gif)
LOL
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
An IF statement can be pain in the ass to create. As can LET (or no LET
).
the most easy form of IF THEN to create is like this:
IF something=something THEN linenumber
that jumps to a line number if the condition is true
If you want to create END IF, then you will need to count the number of IF statements on a stack (variable) and decrement it as they are completed. So as you go through each IF like so...
If a=b then
If b=c then
If c=d then
print "COOL!"
Endif
Endif
Endif
...You will just check the condition. If false, then move on to the nearest Endif without executing any code. Then do the next one.
If you dont plan of "stacking" IFs and having them work properly, then you could just jump to the nearest END IF.
That probably was not the BEST description, but it should give you an idea...
![Laughing :lol:](./images/smilies/icon_lol.gif)
the most easy form of IF THEN to create is like this:
IF something=something THEN linenumber
that jumps to a line number if the condition is true
If you want to create END IF, then you will need to count the number of IF statements on a stack (variable) and decrement it as they are completed. So as you go through each IF like so...
If a=b then
If b=c then
If c=d then
print "COOL!"
Endif
Endif
Endif
...You will just check the condition. If false, then move on to the nearest Endif without executing any code. Then do the next one.
If you dont plan of "stacking" IFs and having them work properly, then you could just jump to the nearest END IF.
That probably was not the BEST description, but it should give you an idea...
I found a way how to make IF command with 2 variables just like told me, but i have trouble with GOTO. It would work like this:
But I don't know how to make GOTO because the program would need to search the code for the "start:" and then execute the code beneath it. And that i don't really know how to do. Any help bongomeno?
Code: Select all
start:
variable1 = 2
variable2 = 3
IF,variable1<variable2,THEN,GOTO,start:
- BigBadKing
- Veteran
- Posts: 83
- Joined: Fri Oct 09, 2009 12:39 pm
- Location: Space
- Contact:
some help is available. first, include these three lines at the start
of your program:
the third line is used to say how many labels we defined. insert the
following snippet of code before where you begin to translate commands
and do what they say:
replace TotalLineNumbers with the variable which you use to store
how many lines the program is. and also replace Line with the string
array on which you use to store the program. now, whenever you
need to jump to a specific label. use the following code:
replace count with the variable you use to loop through the program.
if anything is not clear then just ask!
of your program:
Code: Select all
dim shared Labels$(1000)
dim shared LineNum(1000)
GOTOs = 0
following snippet of code before where you begin to translate commands
and do what they say:
Code: Select all
for i = 0 to TotalLineNumbers
' if at the end of the line is a colon, then make a label
if right$(Line$(i), 1) = ":" then
GOTOs = GOTOs + 1
LineNum(GOTOs) = i
Labels$(GOTOs) = Line$(i)
end if
next i
how many lines the program is. and also replace Line with the string
array on which you use to store the program. now, whenever you
need to jump to a specific label. use the following code:
Code: Select all
for i = 1 to GOTOs
if Labels$(GOTOs) = Line$(count+1) then count = LineNum(GOTOs)
next i
if anything is not clear then just ask!
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>
You dont need a goto statement or a label. You could just follow classic BASIC syntax like this:
10 LET X=X+1
20 PRINT X
30 IF X<10 THEN 10
That program would count to 10, but the IF statement only jumps to the line.
So all you would have to do is parse out the line number.
If your code array is a max of 100 lines, then it would be 0-99.
So you take the line number in the IF statement and subtract 1 and move your line pointer to that number.
For an actual code example, you can check my site for my TINY BASIC interpreter in the downloads, it might answer a few of your questions.
All code written in it can be run under Qbasic without any modification.
10 LET X=X+1
20 PRINT X
30 IF X<10 THEN 10
That program would count to 10, but the IF statement only jumps to the line.
So all you would have to do is parse out the line number.
If your code array is a max of 100 lines, then it would be 0-99.
So you take the line number in the IF statement and subtract 1 and move your line pointer to that number.
For an actual code example, you can check my site for my TINY BASIC interpreter in the downloads, it might answer a few of your questions.
All code written in it can be run under Qbasic without any modification.
- BigBadKing
- Veteran
- Posts: 83
- Joined: Fri Oct 09, 2009 12:39 pm
- Location: Space
- Contact:
i think a working example will do it:
i hope this explains something, to make it work. compile it
and make a file named on whatever you like, then make the
contents of the file like this:
ALT+ENTER.
i hope this is clear.
Code: Select all
dim Labels$(1000)
dim LineNum(1000)
dim Line$(1000)
GOTOs = 0
LengthOfFile = 0
open command$ for input as #1
while not eof(1)
input #1, Line$(LengthOfFile)
Line$(LengthOfFile) = ucase$(Line$(LengthOfFile))
LengthOfFile = LengthOfFile + 1
wend
close #1
for count = 0 to TotalLineNumbers
' if at the end of the line is a colon, then make a label
if right$(Line$(count), 1) = ":" then
GOTOs = GOTOs + 1
LineNum(GOTOs) = count
Labels$(GOTOs) = Line$(count)
end if
next count
for count = 0 to LengthOfFile
select case Line$(count)
case "GOTO"
' GOTO code is here
for i = 1 to GOTOs
if Labels$(GOTOs) = Line$(count+1) then count = LineNum(GOTOs)
next i
' Anything else goes here
end select
next count
and make a file named on whatever you like, then make the
contents of the file like this:
warning: this enters into an infinite loop, to stop it. press thetest:
goto,test:
ALT+ENTER.
i hope this is clear.
Last edited by BigBadKing on Thu Mar 04, 2010 11:01 am, edited 1 time in total.
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>
- BigBadKing
- Veteran
- Posts: 83
- Joined: Fri Oct 09, 2009 12:39 pm
- Location: Space
- Contact:
BigBadKing, your code works perfectly, but when I add
after GOTO part it doesn't work. Why?
Code: Select all
CASE "beep"
BEEP
after GOTO part it doesn't work. Why?
- BigBadKing
- Veteran
- Posts: 83
- Joined: Fri Oct 09, 2009 12:39 pm
- Location: Space
- Contact:
i tried Beep personally, it doesnt work for me either. just
BEEP doesnt work, i tried everything else and they all worked.
if you want a print command:
any questions?
btw, i think BEEP doesnt work if the PC doesnt support PC
SPEAKER. mine doesnt anyway. :(
BEEP doesnt work, i tried everything else and they all worked.
if you want a print command:
Code: Select all
case "PRINT"
if Line$(count+1) <> "" then print Line$(count+1) else print
btw, i think BEEP doesnt work if the PC doesnt support PC
SPEAKER. mine doesnt anyway. :(
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>
But what i have more inputs in a command like this:
Code: Select all
sound,2,400