Shop OBEX P1 Docs P2 Docs Learn Events
Why does this code work this way?.... — Parallax Forums

Why does this code work this way?....

DerPhilosophDerPhilosoph Posts: 2
edited 2014-03-19 12:59 in BASIC Stamp
New forum member here. Finally got through the "What's a microcontroller" kit with the BS2 after working on it here and there for about 2 yrs. I understood the code presented in the first 9 and a half chapters, but something in Chp 10 "Running the Whole Show" had me scratching my head. The code is for the activity where you make a simple password checker program. I haven't been able to figure out how the index ever reaches the value of 5. The code below is exactly (pretty much) the code in the book and the program works as intended. However the book didn't explain how index reaches the value of 5, which causes the DO LOOP UNTIL to stop...

In debugging it, I placed debug commands in the FOR..NEXT loop and then right after the FOR..NEXT loop (just before the conditional statement IF index <> 5 THEN). By doing so, I was able to see that only after the correct password ("pass!") was entered did the index become equal to 4 at the end of the FOR..NEXT loop.. That much was obvious looking at the code, but what was not obvious was how index suddenly became equal to 5 just before it went to the conditional statement !!!

How did that happen???


I have pasted the code below for your reference:

' {$STAMP BS2}
' {$PBASIC 2.5}

'check password entered in Debug Terminal's transmit window

'
[DATA Directives]

Password DATA "pass!" 'store "secret" password here

'
[Variable Declarations]

index VAR Nib
temp VAR Byte 'stores single character
userEntry VAR Byte(5) 'store user entered string


'
[Initialization Routine]

GOSUB Check_Password

'
[Main Routine]
'No main routine for this program

DEBUG CR, "All Done"

END

'
[Subroutine: Check for correct password]

Check_Password:

DO

DEBUG "Enter password: " 'user instructions

DEBUGIN STR userEntry \5 'get user input

FOR index = 0 TO 4 'check array against DATA; get next password char, compare to user input
READ Password + index, temp
IF temp <> userEntry(index) THEN EXIT 'exit if not equal
NEXT

IF index <> 5 THEN 'if exit then index not equal to 5
DEBUG CR,"Password not correct.",CR
ENDIF

LOOP UNTIL index = 5



DEBUG CR, "Password is correct;",CR,
"program can continue..."


RETURN

Comments

  • GenetixGenetix Posts: 1,754
    edited 2014-03-19 09:16
    Welcome to the forums DerPhilosoph.
    I see that you have What's a Microcontroller (WAM) 2.2 and looking at the code for PasswordChecker.bs2 and ReusablePasswordChecker.bs2
    Looking at that code segment, I agree with you that the index will never reach 5 since there is nothing to make to go passed 4.
    I think the IF-THEN and LOOP-UNTIL should use an index value of 4, not 5.

    I will try running this program later today. Good Catch!
  • Mike GreenMike Green Posts: 23,101
    edited 2014-03-19 09:25
    The FOR/NEXT statement increments the loop variable at the end of the loop (index) and checks it against the upper bound of the loop. If it's greater than the upper bound, the loop exits. That leaves the loop variable, in this case, at 5. The IF statement is intended to check whether the loop exits because a password matches or whether none of the passwords match.
  • DerPhilosophDerPhilosoph Posts: 2
    edited 2014-03-19 12:59
    Thanks for the clarification Mike Green. I must have been having a mental block or what not to not realize that the FOR..NEXT loop operated that way.
Sign In or Register to comment.