Jump to content
IGNORED

While loop in Oscar' s book example


Alkadian

Recommended Posts

Hi there,

 

I am moving my first steps in this fantastic world of programming the Intellivision.

 

I have been following Oscar's books (well I have started from his first book) and I am bit unclear about the following lines of code for one of his examples in the book (Game Title -  As soon as you press a button music starts playing)

 

DO

WAIT

C=CONT1

LOOP WHILE C

 

DO

WAIT

C=CONT1

LOOP WHILE C=0

 

I understand that this has to do with the pressing/releasing of any buttons of the controllers. But I don't fully understand how both loops work.

 

Many thanks in advance for any kind assistance!

 

Edited by Alkadian
Link to comment
Share on other sites

Total guess as I don’t program on intv but looks like first loop basically loops while c is true, so cont1 must be a true value which could be any non negative value possibly, guessing based on second example it defaults to 0. So in one when controller is moved it sets c to a non true value and in second example it sets it to non zero value. 

Link to comment
Share on other sites

Thanks!

What I don't get is that if I don't press any button CONT1 should be zero and I should be exiting the first loop as c is not true.

The way I read the first loop is "wait till any button is pressed, then exit and move to the second loop".

I am sure I am missing the obvious here!

Edited by Alkadian
Link to comment
Share on other sites

boolean can be true or false but it can also be numeric. zero =true, negative =false. different language use different numeric values.   some may use 0 and 1 some may use non negative = true, negative = false. i’ve never seen his book or the intv basic language so i don’t know, but either his book or the basic docs should have it documented. 

Link to comment
Share on other sites

As you may have seen in the book, there are various kinds of loops.  The particular kind in your question has very specific characteristics.


First, it is a "do" loop, which is typically used when you do not know how many times you need to repeat.

 

Second, it has the "loop condition" at the bottom, which means that it will enter the body of the loop at least once, then test to see if it is done.  This is different from testing at the top, which means that it will check to see if the condition for looping is true even before entering the loop for the first time.

 

Third, it uses a "while" test condition, which means that the test must evaluate to true for the loop to repeat.  In other words, it will loop while the test evaluates to true, and exit when it is no longer true.  Contrast this to the "until" condition which will loop until the test condition evaluates to true, at which point it will stop repeating.


One thing to bear in mind is that in IntyBASIC, a test condition is evaluated by comparing it to zero, which is interpreted as false.  This means that when the condition expression evaluates to any other value that is not zero, it will be interpreted as true.

 

In short, zero means false; anything else -- absolutely anything else:  positive, negative, whatever -- means true.

 

So, with all this at hand, let us see what the loop does:

 

DO
  WAIT
  C=CONT1
LOOP WHILE C

 

Because the test condition is at the end, the code inside the loop will be executed at least the first time.  It will first wait for the next video frame, then get the current state of the hand-controller into the variable C.  It will then test if the value is not-zero.  Because it uses "while" it will loop only while the value is true, that is, not zero.

 

In other words, it will loop while the controller is not idle (i.e., not zero).  This is typically done to wait for the player to release the controller before accepting a new input.
 

If the controller is idle, the value of C would be zero, and the loop would exit on the very first test, and continue with the program without looping.

 

The second loop,
 

DO
  WAIT
  C=CONT1
LOOP WHILE C=0


does a similar thing, except that the test now is to see if C is zero.  This means that the loop will iterate while the controller is idle (C is zero, which makes the condition true) and exit when there is an actual value (C is not zero, which makes the condition false).

 

I hope this helps.  In the future, may I suggest posting your programming questions in the Programming Forum.  There are many people willing to help there, and we don't bite. :)

 

   dZ.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

21 minutes ago, evg2000 said:

boolean can be true or false but it can also be numeric. zero =true, negative =false. different language use different numeric values.   some may use 0 and 1 some may use non negative = true, negative = false. i’ve never seen his book or the intv basic language so i don’t know, but either his book or the basic docs should have it documented. 


In IntyBASIC proper true is -1 ($FFFF), and false is zero.  However, the -1 value is a compiler convention used as the result of Boolean evaluation, when the output is being assigned to a variable or used as an operand into a larger expression.  In reality, any non-zero value is true -- the compiler will generate code to return -1 when so.

 

This is convenient because it makes true the complement of false, so that "NOT true" evaluates to "false," and vice-versa; allowing the use of bitwise operations for logic expressions.

 

    dZ.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

20 minutes ago, DZ-Jay said:


In IntyBASIC proper true is -1 ($FFFF), and false is zero.  However, the -1 value is a compiler convention used as the result of Boolean evaluation, when the output is being assigned to a variable or used as an operand into a larger expression.  In reality, any non-zero value is true -- the compiler will generate code to return -1 when so.

 

    dZ.

Thank you so much for finding the time to respond in such a very detailed way! All clear now, much appreciated!

 

My apologies for posting in the wrong thread! My bad! I will keep it in mind for the future as I know I will have more questions. 

 

Thanks again for your comprehensive response!

Edited by Alkadian
  • Like 1
Link to comment
Share on other sites

21 minutes ago, evg2000 said:

boolean can be true or false but it can also be numeric. zero =true, negative =false. different language use different numeric values.   some may use 0 and 1 some may use non negative = true, negative = false. i’ve never seen his book or the intv basic language so i don’t know, but either his book or the basic docs should have it documented. 

Thanks.

Link to comment
Share on other sites

1 hour ago, Alkadian said:

Thank you so much for finding the time to respond in such a very detailed way! All clear now, much appreciated!

 

My apologies for posting in the wrong thread! My bad! I will keep it in mind for the future as I know I will have more questions. 

 

Thanks again for your comprehensive response!


No worries, it is my pleasure.  I am only paying forward all the help I received in the past from the others. :)

 

     dZ.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...
On 9/12/2023 at 12:59 PM, Alkadian said:

Thank you so much for finding the time to respond in such a very detailed way! All clear now, much appreciated!

 

My apologies for posting in the wrong thread! My bad! I will keep it in mind for the future as I know I will have more questions. 

 

Thanks again for your comprehensive response!

No worries about posting in the "wrong" thread.  We indie devs just spend more time in the development section to help out with questions like this.  If you need more help, feel free to reach out.

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...