IF

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

IF

Post by izidor » Mon Mar 01, 2010 4:54 pm

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?

User avatar
burger2227
Veteran
Posts: 2420
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Mon Mar 01, 2010 5:06 pm

IF you are unfamiliar with IF...END IF, you should not be creating another programming language. You should USE another programming language! :roll:

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

User avatar
bongomeno
Veteran
Posts: 231
Joined: Wed Dec 10, 2008 9:08 am
Location: San Bernardino CA
Contact:

Post by bongomeno » Mon Mar 01, 2010 11:17 pm

An IF statement can be pain in the ass to create. As can LET (or no LET :lol: ).

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...
Home: http://www.geekbasic.com" target="_blank
Forum: http://www.geekbasic.com/index.gbws?$page=forumsmain" target="_blank
QB64 powered web server: http://www.geekbasic.com/index.gbws?$pa ... inter=%201" target="_blank

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor » Tue Mar 02, 2010 11:50 am

@burger2227 If anyone ever said that you are funny I would bet that he had brain damage.

@bongomeno Thanks for the idea!

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor » Tue Mar 02, 2010 12:53 pm

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:

Code: Select all

start:
variable1 = 2
variable2 = 3
IF,variable1<variable2,THEN,GOTO,start:
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?

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Tue Mar 02, 2010 1:16 pm

some help is available. first, include these three lines at the start
of your program:

Code: Select all

 dim shared Labels$(1000)
 dim shared LineNum(1000)
 GOTOs = 0
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:

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
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:

Code: Select all

 for i = 1 to GOTOs
  if Labels$(GOTOs) = Line$(count+1) then count = LineNum(GOTOs)
 next i
replace count with the variable you use to loop through the program.

if anything is not clear then just ask!
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor » Tue Mar 02, 2010 1:53 pm

Ok, I understand most of it. When my program reads a line that starts with "IF" it stores and checks variables and other things, it stores the name of the label (witch in my code example is "start:") in Label$. And my question is how to link (connect) Label$ with third part of your code. Thanks!

User avatar
bongomeno
Veteran
Posts: 231
Joined: Wed Dec 10, 2008 9:08 am
Location: San Bernardino CA
Contact:

Post by bongomeno » Tue Mar 02, 2010 8:40 pm

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.
Home: http://www.geekbasic.com" target="_blank
Forum: http://www.geekbasic.com/index.gbws?$page=forumsmain" target="_blank
QB64 powered web server: http://www.geekbasic.com/index.gbws?$pa ... inter=%201" target="_blank

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor » Wed Mar 03, 2010 7:01 am

Link for TinyBASIC doesn't work :(

User avatar
bongomeno
Veteran
Posts: 231
Joined: Wed Dec 10, 2008 9:08 am
Location: San Bernardino CA
Contact:

Post by bongomeno » Wed Mar 03, 2010 9:50 am

I had the same problem, you have to right click and do Save As for some wierd reason.
Home: http://www.geekbasic.com" target="_blank
Forum: http://www.geekbasic.com/index.gbws?$page=forumsmain" target="_blank
QB64 powered web server: http://www.geekbasic.com/index.gbws?$pa ... inter=%201" target="_blank

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor » Wed Mar 03, 2010 12:59 pm

@bongomeno, can i e-mail you my code so you can see what is my problem?

User avatar
bongomeno
Veteran
Posts: 231
Joined: Wed Dec 10, 2008 9:08 am
Location: San Bernardino CA
Contact:

Post by bongomeno » Wed Mar 03, 2010 8:34 pm

sure, I will be glad to take a look at it!
Home: http://www.geekbasic.com" target="_blank
Forum: http://www.geekbasic.com/index.gbws?$page=forumsmain" target="_blank
QB64 powered web server: http://www.geekbasic.com/index.gbws?$pa ... inter=%201" target="_blank

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Thu Mar 04, 2010 8:37 am

i think a working example will do it:

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
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:
test:
goto,test:
warning: this enters into an infinite loop, to stop it. press the
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>

User avatar
bongomeno
Veteran
Posts: 231
Joined: Wed Dec 10, 2008 9:08 am
Location: San Bernardino CA
Contact:

Post by bongomeno » Thu Mar 04, 2010 9:52 am

You ould just press CTRL+BREAK to return to the code. Doesnt ALT+ENTER exit?
Home: http://www.geekbasic.com" target="_blank
Forum: http://www.geekbasic.com/index.gbws?$page=forumsmain" target="_blank
QB64 powered web server: http://www.geekbasic.com/index.gbws?$pa ... inter=%201" target="_blank

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Thu Mar 04, 2010 10:59 am

Bongomeno, you didnt notice that i said COMPILE the example
i gave. CTRL+BREAK works only in the QB environment.
<a href="http://mccorp.orgfree.com">Macselent Corporation Offical Site</a>

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor » Thu Mar 04, 2010 11:58 am

BigBadKing, your code works perfectly, but when I add

Code: Select all

CASE "beep"
BEEP

after GOTO part it doesn't work. Why?

User avatar
BigBadKing
Veteran
Posts: 83
Joined: Fri Oct 09, 2009 12:39 pm
Location: Space
Contact:

Post by BigBadKing » Fri Mar 05, 2010 3:37 am

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:

Code: Select all

 case "PRINT"
  if Line$(count+1) <> "" then print Line$(count+1) else print
any questions?
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>

izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor » Fri Mar 05, 2010 9:28 am

But what i have more inputs in a command like this:

Code: Select all

sound,2,400

Post Reply