• Print

Author Topic: LOOP UNTIL  (Read 182 times)

LINUXQB64gltester

  • Full Member
  • ***
  • Posts: 241
    • Anyone that likes Art
LOOP UNTIL
« on: March 16, 2013, 07:13:24 AM »
 I know I can look at the Wiki, too on this, and I am going to, but I decided to also post this question,
about, LOOP UNTIL
Code: [Select]
  LOOP UNTIL INKEY$ = CHR$ (27)
 OK, I understand how this is used to end a loop, or a program, when the escape key
Code: [Select]
CHR$ (27) is hit,
 What I would like to do is also include a option,  so that if the "space" key or "return" (enter) is hit, instead of ending the program, and exiting, it goes on to the next part, or another program,.
 Or, goes back to a menu, If it is possible, I would like to keep it where, the entire program can be ended , with the escape key, if the space key is used, it goes to a menu, or the next program, , my thoughts at the moment though, best to just go to a menu.
Example,
Code: [Select]
LOOP UNTIL INKEY$ = CHR$ (27)
IF INKEY$= (CHR$ ("spacekey") THEN
' Return or Goto,Gosub, or whatever, to go back to the menu
END IF
I don't know what the number for the CHR value would be for the space key,   off hand,
It is hard for me, to express this question,..sorry,.. I suppose in other words, 
 What is the best way to end a loop, and go either on to the next program, or back to the "menu", where it started from ?
  Hope this makes sense,...and thanks from Garry
For off topic discussions :
Non-QB64 Related Discussion Forum
Also
My Experimenting Forum
"QB64 has a great future!"

SMcNeill

  • Hero Member
  • *****
  • Posts: 2602
    • Email
Re: LOOP UNTIL
« Reply #1 on: March 16, 2013, 07:26:59 AM »
I'm not at the computer ATM, but this is how I handle what you're looking for:


Do
    A$ = Inkey$
    Select case a$
          Case chr$(27):  exit do
          Case chr$(32):  call spacehandlersub
    End select
Loop

Chr$(32) is the space key.   Just assign inkey$ to a set value (a$ in my example), and select case what you want it to do afterwards.   Simple.  :)
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files

OlDosLover

  • Hero Member
  • *****
  • Posts: 4116
  • OlDosLover
    • Email
Re: LOOP UNTIL
« Reply #2 on: March 16, 2013, 07:30:05 AM »
Hi all,
    Here's an example
Code: [Select]
REM
SCREEN 12
Done = 0
DO
  A$ = INKEY$
  IF LEN(A$) <> 0 THEN A$ = LEFT$(A$, 1)
  SELECT CASE A$
    CASE CHR$(13)
      GOSUB Enter
    CASE CHR$(27)
      Done = 1
    CASE CHR$(32)
      GOSUB SpaceBar
  END SELECT
LOOP UNTIL Done = 1
PRINT "We have used ESCAPE to exit"
SLEEP
SYSTEM

Enter:
PRINT "We hit the ENTER key"
RETURN

SpaceBar:
PRINT "We hit the space bar key"
RETURN
OlDosLover.

mcalkins

  • Hero Member
  • *****
  • Posts: 1297
    • qbasicmichael.com
    • Email
Re: LOOP UNTIL
« Reply #3 on: March 16, 2013, 09:03:42 AM »
INKEY$ removes keystrokes from the input buffer. So, for example:

Code: [Select]
DO
 IF "x" = INKEY$ THEN dosomething
 IF "y" = INKEY$ THEN dosomething
LOOP

In this case the first INKEY$ might remove a "y" from the buffer. Since "y" is not "x", the first condition is false. But then, the "y" is no longer on the buffer, so the second condition is false also.

That is why, if you need to do something like that, you would read INKEY$ once, and store it in a variable:

Code: [Select]
DO
 a$ = INKEY$
 IF "x" = a$ THEN dosomething
 IF "y" = a$ THEN dosomething
LOOP

This way, you won't miss any keystrokes. The important thing to remember here is that you should only read INKEY$ once per iteration of the loop. If you need to use the value multiple times, store it in a variable. However, if you just have a simple SELECT CASE, then you don't need the variable. SMcNeill's example can be rewritten as:

Code: [Select]
DO
 SELECT CASE INKEY$
 CASE CHR$(27): EXIT DO
 CASE " ": spacehandlersub
 END SELECT
LOOP

With OldDosLover's example, I am not sure what the purpose of this line is:

IF LEN(A$) <> 0 THEN A$ = LEFT$(A$, 1)

The length will only ever be 0, 1, or 2. If the length is 1, that doesn't accomplish anything. If the length is 2, you're selecting the left byte, which would always be 0 in that case. The SELECT CASE does not have a CASE for CHR$(0), so, there's not much point.

If you wanted to handle things like arrow keys, you could do something like this:

Code: [Select]
DO
 _LIMIT 40
 a$ = INKEY$
 SELECT CASE a$
 CASE "" ' nothing
 CASE CHR$(&H1B): EXIT DO ' esc
 CASE CHR$(&HD): PRINT
 CASE MKI$(&H4800) ' up
 CASE MKI$(&H4B00) ' left
 CASE MKI$(&H4D00) ' right
 CASE MKI$(&H5000) ' down
 CASE ELSE: PRINT a$;
 END SELECT
LOOP

Here is a program that can tell you what various keystrokes return:

Code: [Select]
_CONTROLCHR OFF
DIM k AS STRING
' DEF SEG = 0
DO
 DO
  _LIMIT 40
  k = INKEY$
 LOOP UNTIL LEN(k)
 PRINT CHR$(&H22); k; CHR$(&H22)
 SELECT CASE LEN(k)
  CASE 1: PRINT "chr$(&h"; LCASE$(HEX$(ASC(k))); ")";
  CASE 2: PRINT "mki$(&h"; LCASE$(HEX$(CVI(k))); ")";
 END SELECT
 PRINT SPACE$(40)
 ' PRINT LCASE$(HEX$(PEEK(&H417)))
LOOP UNTIL k = CHR$(&H1B)

(The commented code reads what would have been a keyboard status byte in DOS.)

You can also use SLEEP before reading INKEY$ in a loop:

Code: [Select]
DO
 SLEEP
 a$ = INKEY$
 PRINT a$;
LOOP UNTIL CHR$(&H1B) = a$

The SLEEP waits for a keystroke, but leaves it on the buffer. Notice that this one doesn't need _LIMIT.

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

LINUXQB64gltester

  • Full Member
  • ***
  • Posts: 241
    • Anyone that likes Art
Re: LOOP UNTIL
« Reply #4 on: March 16, 2013, 10:51:29 AM »
Thanks all of you on this, I have to go out, and do some errands,  when I get back I can try these,
and see what happens,
thanks
from Garry
For off topic discussions :
Non-QB64 Related Discussion Forum
Also
My Experimenting Forum
"QB64 has a great future!"

  • Print