Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on ln in a for/next loop
#11
Quote:name that tune: Tie A Yellow Ribbon Around The Old Oak Tree

Can you name the singer who made it famous?

Tony Orlando and Dawn. I knew them personally. They used to frequent my father's store.

Now a math question:

Q: What does Fake News + Phony Politicians =

A: Real Problems

Pete
Shoot first and shoot people who ask questions, later.
Reply
#12
The global "ln" was probably passed to a Procedure(Sub). - A local "ln" is only known in this subprogram and has no effect on the program outside.

[Image: Variable-Lokal-Global-2024.jpg]

Why it is -2

Does the local variable also come before a "shared variable" in the main program? Test it!  Tongue

Code: (Select All)

'Beispiel lokale Variablen - 13. Sept. 2024

$Console:Only
Option _Explicit

Declare Sub Beispiel()
Declare Sub Beispiel2(ln As Integer)

Dim As Integer ln, speed(10)

For ln = 1 To 10
  speed(ln) = ln
  Print speed(ln)

  'Sonst ist ln = 11
  If ln = 10 Then
    Exit For
  End If
Next
Print: Print ln

Locate CsrLin + 1, 2
Call Beispiel

Locate CsrLin + 1, 2
Call Beispiel2(ln)
End

Sub Beispiel
  Dim As Integer i
  Dim As Single ln

  For i = 1 To 10
    ln = i * 1.36
    Print Using "##.##"; ln
    Locate CsrLin, 2
  Next
End Sub

Sub Beispiel2 (ln)

  Dim As Integer ln

  'Das lokale "ln" geht vor dem globalen
  ln = ln - 2
  Print ln
End Sub
Reply
#13
Why -2? because when you Dim ln as Integer inside the subroutine you set it to zero.

Comment that line out and all is as expected.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#14
@bplus - correct in principle. It is -2 because every new uninitialized variable in Basic is automatically initialized with zero. Zero minus 2 is . . . -2.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  _Putimage Question. Pete 11 671 01-04-2026, 09:33 PM
Last Post: Pete
  Nice simple, I hope, question Mad Axeman 4 328 12-20-2025, 09:28 PM
Last Post: SMcNeill
  Exiting sub while inside a loop PhilOfPerth 5 498 12-05-2025, 09:40 AM
Last Post: PhilOfPerth
  NewBie Question niteflyer 2 322 11-06-2025, 07:11 PM
Last Post: Petr
  A Question About _MAPTRIANGLE Magdha 2 369 11-02-2025, 11:37 AM
Last Post: Magdha

Forum Jump:


Users browsing this thread: 1 Guest(s)