Announcement

Collapse
No announcement yet.

Error 451 For Loop Expected

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error 451 For Loop Expected

    Please add your comments and suggestions as a Reply to this thread.





    PB/WIN - Error 451 - FOR loop expected
    FOR loop expected - A NEXT, EXIT FOR, or ITERATE FOR was encountered here without the associated FOR statement to begin the FOR/NEXT loop.

    Last edited by Gary Beene; 28 Oct 2014, 07:07 PM.

  • #2
    Suggested addition information

    Add the following:
    Code:
    This error is also generated with a legitimate FOR/NEXT block containing one 
    of the following:
    IF/END IF block missing END IF
    DO/LOOP block missing LOOP
    WHILE/WEND block missing WEND 
    SELECT CASE/END SELECT block missing END SELECT
    
    When these statement are missing inside of another block, the best the 
    compiler can do is let you know there is an error within the block as 
    it cannot determine which code should be in which block. This list offers
    suggestions to help find the error.
    Test code: loopconditionalerrors.bas
    Code:
    #COMPILE EXE         'final posted version of this testing code-testing  complete
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
      LOCAL d, f, w, i, s, u AS LONG
      LOCAL boring AS STRING
      LOCAL tWin AS DWORD
      RANDOMIZE TIMER
      TXT.WINDOW("Loop-conditional error check", 5,5, 40,90) TO tWin
      TXT.PRINT
      d=1
      f=2
      w=3
      i=4
      s=5
      TXT.PRINT "Inside a DO/LOOP"
      DO
        IF s>4 THEN
          INCR d
          TXT.PRINT " TEST  1 Missing END IF inside a DO/LOOP error 448 message is misleading"
        END IF                'without this compiler error 448:"DO loop expected"
        FOR u=1 TO 3
          f=d
          TXT.PRINT " TEST  2 Missing NEXT inside a DO/LOOP error 448 message is misleading"
          EXIT FOR
        NEXT u                'without this compiler error 448:"DO loop expected"
        WHILE ISTRUE w
          TXT.PRINT " TEST  3 Correct error message but the cursor can be far afield!!"
          w=0
        WEND                  'without this compiler error 456:"LOOP/WEND expected-no issue"
        SELECT CASE AS LONG s
          CASE 5
            TXT.PRINT " TEST  4 Missing END SELECT inside a DO/LOOP error 448 message is misleading"
          CASE ELSE
        END SELECT            'without this compiler error 448:"DO loop expected"
      LOOP UNTIL d=2
    
      TXT.PRINT
      TXT.PRINT "Inside a WHILE/WEND"
      w=3
      WHILE w=3
        IF s>4 THEN
          INCR d
          TXT.PRINT " TEST  5 Missing END IF inside a WHILE/WEND error 448 message is misleading"
        END IF              'without this compiler error 448:"DO loop expected"
        FOR u=1 TO 3
          f=d
          TXT.PRINT " TEST  6 Missing NEXT inside a WHILE/WEND error 448 message is misleading"
          EXIT FOR
        NEXT u              'without this compiler error 448:"DO loop expected"
        DO
          TXT.PRINT " TEST  7 Correct error message but the cursor can be far afield!!"
          EXIT DO
        LOOP                'without this compiler error 456:"LOOP/WEND expected"
        SELECT CASE AS LONG s
          CASE 5
            TXT.PRINT " TEST  8 Missing END SELECT inside a WHILE/WEND error 448 message is misleading"
          CASE ELSE
        END SELECT            'without this compiler error 448:"DO loop expected"
        EXIT LOOP
      WEND
    
      TXT.PRINT
      TXT.PRINT "Inside a FOR/NEXT"
      w=0
      FOR u=1 TO 1
        IF s>4 THEN
          INCR d
          TXT.PRINT " TEST  9 Missing END IF inside a FOR/NEXT error 451 message is misleading"
        END IF              'without this compiler error 451:"FOR loop expected"
        WHILE ISFALSE w
          TXT.PRINT " TEST 10 Missing WEND inside a FOR/NEXT error 451 message is misleading"
          w=3
        WEND                'without this compiler error 451:"FOR loop expected"
        SELECT CASE AS LONG s
          CASE 5
            TXT.PRINT " TEST 11 Missing END SELECT inside a FOR/NEXT error 451 message is misleading"
          CASE ELSE
        END SELECT          'without this compiler error 451:"FOR loop expected"
        DO
          TXT.PRINT " TEST 12 Missing LOOP inside a FOR/NEXT error 451 message is misleading"
          EXIT DO
        LOOP                'without this compiler error 451:"FOR loop expected"
      NEXT u
    
      TXT.PRINT
      TXT.PRINT "Inside an IF THEN/END IF"
      w=0
      IF ISTRUE i THEN
        FOR u=1 TO 3
          f=d
          TXT.PRINT " TEST 13 Missing NEXT inside a IF THEN/END IF error 447 message is misleading"
          EXIT FOR
        NEXT u              'without this compiler error 447:"IF expected"
        DO
          TXT.PRINT " TEST 14 Missing LOOP inside a IF THEN/END IF error 447 message is misleading"
          EXIT DO
        LOOP                'without this compiler error 447:"IF expected"
        SELECT CASE AS LONG s
          CASE 5
            TXT.PRINT " TEST 15 Missing END SELECT inside a IF THEN/END IF error 447 message is misleading"
          CASE ELSE
        END SELECT          'without this compiler error 447:"IF expected"
        WHILE ISFALSE w
          TXT.PRINT " TEST 16 Missing WEND inside a IF THEN/END IF error 447 message is misleading"
          w=3
        WEND                'without this compiler error 447:"IF expected"
      END IF
    
      TXT.PRINT
      TXT.PRINT "Inside a SELECT CASE/END SELECT"
      w=0
      SELECT CASE AS LONG s
        CASE 5
          IF s>4 THEN
            INCR d
            TXT.PRINT " TEST 17 Missing END IF inside a SELECT CASE/END SELECT error 449 message is misleading"
          END IF              'without this compiler error 449:"SELECT expected"
          FOR u=1 TO 3
            f=d
            TXT.PRINT " TEST 18 Missing NEXT inside a SELECT CASE/END SELECT error 449 message is misleading"
            EXIT FOR
          NEXT u              'without this compiler error 449:"SELECT expected"
          DO
            TXT.PRINT " TEST 19 Missing LOOP inside a SELECT CASE/END SELECT error 449 message is misleading"
            EXIT DO
          LOOP                'without this compiler error 449:"SELECT expected"
          WHILE ISFALSE w
            TXT.PRINT " TEST 20 Missing WEND inside a SELECT CASE/END SELECT error 449 message is misleading"
            w=3
          WEND              'without this compiler error 449:"SELECT expected"
        CASE ELSE
    
      END SELECT
    
      boring= TXT.WAITKEY$
      TXT.END
    
    END FUNCTION
    An image of the code output can be seen in this thread.
    Last edited by Rodney Hicks; 31 Aug 2014, 03:04 AM.
    Rod
    "To every unsung hero in the universe
    To those who roam the skies and those who roam the earth
    To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

    Comment


    • #3
      I'm converting an old GW-BASIC program to PowerBasic 10. I keep getting an error 451 in regards to the following line:

      Error 451 in C:\PBWin10\bin\BauerProp_WorkingFile__compilePowerBASIC.bas(101:017): FOR loop expected
      Line 101: 207 PRINT SN: FOR S=1 TO SN :WB(S)=WB*SF(S) :NEXT

      And yes, for now, I'm keeping the old line numbers. Please don't hate me!

      The variable SN is, depending on what's going on, equal to a whole number, either 1,2,3 ..or11. Just that.

      Thank you for any help you can give me.

      Mike

      Comment


      • #4
        This question should probably be in "PowerBASIC for Windows" section. This section is on-line copy of help itself.

        Errors are not always what they seem. In this case I think it is PRINT. There is no PRINT in PBWin. The closest is PRINT# for output to file. From the one line of code I can't tell if you need CONTROL ADD TEXT or a MSGBOX.

        Cheers,

        P.S. numeric SN will need to be converted to a string with STR$, FORMAT$ or other to string functions.

        P.P.S. If you create a TXT.WINDOW, then TXT.PRINT is closer to what you're starting with. Or, do you also have PBCC?
        Last edited by Dale Yarker; 6 Oct 2017, 12:27 PM.
        Dale

        Comment


        • #5
          Mike,
          In the recent versions of the PB compilers, I think the syntax of the FOR/NEXT got more strict.
          Try changing:
          Code:
          FOR S=1 TO SN :WB(S)=WB*SF(S) :NEXT
          to:
          Code:
          FOR S=1 TO SN :WB(S)=WB*SF(S) :NEXT S
          IOW, "NEXT" needs to be "NEXT S"

          -John

          Comment


          • #6
            Summarized from John and Dale
            try

            Code:
            DIM WBm() AS LONG
            207  ?  " SN " + STR$(SN)
                  REDIM WBm( 1 TO SN)
                  FOR S=1 TO SN :WBm(S)=WB*SF(S) :NEXT S
            Make sure that you have REDIM WBm( 1 TO SN)

            As your old program has WB and WB() which can be confusing to the compiler

            Comment


            • #7
              Try breaking the line up and see where it throws the error, as
              Code:
              207 PRINT SN
                 FOR S=1 TO SN
                   WB(S)=WB*SF(S)
                 NEXT
              John, in PB Win 10 the counter variable is not required with NEXT in a FOR/NEXT loop. Help shows it as optional.
              Rod
              "To every unsung hero in the universe
              To those who roam the skies and those who roam the earth
              To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

              Comment


              • #8
                Just check Help for syntax.

                Cheers,
                Dale

                Comment

                Working...
                X