2

I'm passing a boolean expression in terms of some variable I into a TI-BASIC program that manipulates I, but the boolean expression is only evaluated once - at the beginning of the program's execution.

Here is a sample program:

Prompt J
0 -> I

Lbl 1
1 + I -> I
Disp I

If J
Then
Goto 2

Else
Goto 1
End

Lbl 2
End

(This is not the actual program I'm writing, which is more complicated and thus cannot use a for loop; the above program could obviously be written more elegantly with a for loop.)

We increment I by 1, starting with the number 0. After every increment, we check whether J is true, and if so, we stop.

If I pass in J=I≠5, the program stops after printing 1.

If I pass in J=I=5, the result is not expected. I intend for the program to stop when I=5 is true, but instead the program continues indefinitely. This means that J is only evaluated at the beginning of the program.

Is it possible to reevaluate J in every execution of that loop?


I assume that J is being replaced with 0 and 1 as soon as it is passed in. To rephrase my question, is it possible to input/keep the boolean expression as an unevaluated string/literal? Then I would just call something like eval(J) within the program.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173

3 Answers3

2

The assumption in your update is correct; The J is being replaced by a numerical value.

The answer to your rephrased question is yes, you can pass an expression, but you have to use a function variable instead of a real variable:

Prompt Y1

Whenever Y1 is called, it will re-evaluate, so if you store I=5 to Y1, then Y1 will return 0 whenever I≠5 and 1 when I=5.

Vaelus
  • 1,065
  • 10
  • 27
0
DelVar IPrompt J

Yes I meant to not put a newline there. You don't need one. Trust me.

While not(J
I+1->I
Disp I
End

This is your program, optimized. Your problem is that J is never changed after your original prompt, so the loop will go around and around forever. Yes, you can reevaluate J in every iteration, but you do need to actually DO it, if you want your program to work.

Roguebantha
  • 814
  • 4
  • 19
0

I don't know why you're using Lbl/Goto loops here; I would advise that if a For loop doesn't work for you, then try to use While or Repeat loops instead. They're neater and faster.

With that aside,

You're incrementing I by 1 and checking J≠0 every time. I and J are entirely different variables, and they're not going to interact with each other unless you make them interact. If your initial input is J=0, then the program will run forever.

I intend for the program to stop when I=5 is true, but instead the program continues indefinitely.

But there is nothing in the code that checks if I=5. You should put an If I=5 somewhere if you want this to happen.

Is it possible to reevaluate J in every execution of that loop?

Yes, but you need to manually tell the calculator to do so.

I assume that J is being replaced with 0 and 1 as soon as it is passed in.

This is not the case. Rather, the If J in your code will output FALSE if J=0, and TRUE for any other value of J.

Also, you don't need the End at the end of your program; in TI-BASIC, the End command indicates the end of code blocks such as If:Then conditionals and For/While/Repeat loops.

I suggest you read here: http://tibasicdev.wikidot.com

user3932000
  • 671
  • 8
  • 24