locked
Exiting For Loops Early RRS feed

  • Question

  • Here is a subroutine that I am using to display help messages onscreen. It works perfectly. Here is the problem: There are several of these messages that need to appear throughout the program. I am trying to figure out a way that I can write one subroutine to do all of the help messages. Otherwise it will be necessary to write a sub for each of the help messages and that would greatly the size of the program.

    The first for loop is merely to load the help file into an array. In the main program this will be eliminated as the array is loaded at the front of the program.

    Sub SetAccountHelp
      ClearMsgArea()
      For i = 1 to NumLine
      CheckHelp[i] = File.ReadLine(Folder + HelpFile, i)
    endfor
      j = 21
      NumLine = FilePlus.GetNumberOfLines(Folder + HelpFile)
      For i = 1 To NumLine
        If CheckHelp[i] = ";AccountCodes" Then
          Index = i + 1
          i = NumLine
        EndIf
      EndFor
      TextWindow.BackgroundColor = "darkblue"
      TextWindow.ForegroundColor = "Green"
      For i = Index To NumLine
        If CheckHelp[i] = ";end" Then
          Goto AccountStop
        EndIf
        TextWindow.CursorLeft = 1
        TextWindow.CursorTop = j
        TextWindow.WriteLine(CheckHelp[i])
        j = j + 1
      EndFor
       AccountStop:
      TextWindow.BackgroundColor = "darkblue"
      TextWindow.ForegroundColor = "yellow"
    EndSub

    Wednesday, January 23, 2013 9:39 PM

Answers

  • Here is how I would implement this:

    1) I would declare an array of the help messages

    2) I would create a variable to point at a location in the array

    3) I would finally set the variable to the index in the array of the message I wanted to show and call my sub when needed


    Please mark any answers and "vote as helpful" any posts that help you!

    • Marked as answer by elmo533940 Wednesday, January 23, 2013 10:20 PM
    Wednesday, January 23, 2013 10:12 PM
    Answerer

All replies

  • Here is how I would implement this:

    1) I would declare an array of the help messages

    2) I would create a variable to point at a location in the array

    3) I would finally set the variable to the index in the array of the message I wanted to show and call my sub when needed


    Please mark any answers and "vote as helpful" any posts that help you!

    • Marked as answer by elmo533940 Wednesday, January 23, 2013 10:20 PM
    Wednesday, January 23, 2013 10:12 PM
    Answerer
  • I can see how this would work. My first thought was to make all the help message the same size so that all would be the same length. But I now see that I could use a loop to read the length of each help message into an array to point to the right line number. 

    thanks....

    Wednesday, January 23, 2013 10:22 PM
  • Here is my solution based on you suggestion. However, there is an error here that I am unable to see. The comments should explain what is happening. What I like to do is to have both the program file and a code test file in the IDE so that I can test things before they are put into the program. Seems to save a lot of headaches.

    --> New Edit:

    I have found the solution to the problem. It is how the help messages are tagged in the help file. Previously, the relevant help message began at ;Accounts + 1 and ended at the next ;end - 1. By changing the end tag to :AccountEnd and using that as the trigger to end the LastIndex variable, things now work perfectly with the result that the program code is much smaller.  Thanks to all....

    HelpFile = "CheckBookHelp.txt"
    Folder = "e:\SmallBasic\Progs\"
    Path = Folder + HelpFile
    NumHelp = FilePlus.GetNumberOfLines(Path)

    For i = 1 to NumHelp
      CheckHelp[i] = File.ReadLine(path, i)
    endfor

    Tag = ";Accounts"
    GetHelpIndex()
    SetHelpMsg()

    Sub GetHelpIndex
      For i = 1 To NumHelp
        If CheckHelp[i] = Tag Then
          FirstIndex = i + 1 ' returns 18 which is correct
        EndIf
      EndFor
      TextWindow.WriteLine(FirstIndex) 'prints 18 which is correct
      For i = FirstIndex To NumHelp
        If CheckHelp[i] = ";end" Then
          LastIndex = i - 1 '  should be 34 which is the last line of that help msg
        EndIf
      EndFor
      TextWindow.WriteLine(LastIndex) 'prints 59 which is not correct
    EndSub

    Sub SetHelpMsg  'when implemented this prints from the LastIndex to the end of the help file
      ClearMsgArea()
      TextWindow.BackgroundColor = "darkblue"
      TextWindow.ForegroundColor = "Green"
      TextWindow.CursorLeft = 1
      TextWindow.CursorTop = 21
      For i = FirstIndex To LastIndex
        TextWindow.WriteLine(CheckHelp[i])
      EndFor
    EndSub

    Sub ClearPrompt
      TextWindow.CursorLeft = 1
      TextWindow.CursorTop = 52
      For i = 1 To 50
        TextWindow.Write(" ")
      EndFor
    EndSub

    Sub ClearMsgArea
      TextWindowPlus.ForegroundColor = "darkblue"
      For j = 21 To 50
        For i = 1 To 49
          TextWindowPlus.CursorLeft = i
          TextWindowPlus.CursorTop = j
          TextWindowPlus.Write(" ")
        EndFor
      EndFor
    EndSub


    • Edited by elmo533940 Thursday, January 24, 2013 1:28 AM
    Wednesday, January 23, 2013 11:53 PM
  • For i = 1 to 100
      RandomNumber = Math.GetRandomNumber(100)
      If RandomNumber = 10 Then 'Check if number is = 10 if true exit the loop
         i = 100 'exit loop 
      EndIf
    EndFor

    here is the simple method to exit a for loop. 


    Merry Xmas!

    Monday, January 28, 2013 6:05 PM