Announcement

Collapse
No announcement yet.

Back to BASIC

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

  • Back to BASIC

    I recently download gcc being allured by the "do-it-all" c or c++ libraries. So I set up this simple DO WHILE LOOP with a FOR statement in between for good measure. Geez did I have problems getting out of that FOR block. Sourly missed the good old EXIT FOR. Back to BASIC for me.

    ------------------
    Andre Smit

    [This message has been edited by Andre Smit (edited February 10, 2006).]

  • #2
    Ehm... what about the break instruction?

    Bye!

    ------------------
    Try Online TrID file identifier! Recognize over 1.800 filetypes and counting...
    Give a powerfull scriptable Lua interface to your application with PowerBLua
    Leakout - Firewall outbound control tester
    -- The universe tends toward maximum irony. Don't push it.

    File Extension Seeker - Metasearch engine for file extensions / file types
    Online TrID file identifier | TrIDLib - Identify thousands of file formats

    Comment


    • #3
      Marco - some code snippets to illustrate my point.

      This is the BASIC code:

      Code:
      function pbmain
      
      	local i,j as long
      
      	j=0
      	for i=0 to 10
      		print "i=",i
      		do 
      			j=j+1
      			print "j=",j
      			if j=5 then exit for
      		loop while j < 10	
      	next
      
      	print "i=",i,"j=",j
      	waitkey$
      
      end function
      ... and the c with the break statement that only breaks out of the do loop. I need to break out of the for loop.

      Code:
      #include <stdio.h>
      
      main() {
      
      	int i,j;
      	
      	j=0;
      	
      	for (i=0;i<10;i++) {
      		printf("i=%i\n",i);
      		do {
      			j++;
      			printf("j=%i\n",j);
      			if (j==5) break;
      	 	} while (j<10);
      	}
      	printf("i=%i\tj=%i\n",i,j);
      
      }
      ------------------
      Andre Smit

      Comment


      • #4
        Code:
        #include <stdio.h>
        
        main() {
        
        	int i,j;
        	
        	j=0;
        	
        	for (i=0;i<10;i++) {
        		printf("i=%i\n",i);
        		do {
        			j++;
        			printf("j=%i\n",j);
        			if (j==5) goto breakPoint;
        	 	} while (j<10);
        	}
        breakpoint:
        	printf("i=%i\tj=%i\n",i,j);
        
        }

        ------------------

        Comment


        • #5
          Which proves my point I'll leave it at that.

          ------------------
          Andre Smit

          Comment


          • #6
            If a programmer is going to get into a situation requiring
            an exit from multiple loop levels then a little forethought is warranted.

            1. Have a bailing (exit) strategy involving one or more loop control states to indicate when one or all inner as well as the outer loop should break.
            2. Consider using a function to perform the loopdaloops and return from the function when all necessary processing is complete.
            3. see #1

            Code:
            Function PBMain
             
                Local i,j, done As Long
             
                Do While i<10 And Not done
                    Print "i=",i
                    j=0
                    Do
                        Incr j
                        Print "j=",j
                        If j=5 Then done = True
                    Loop While j < 9 And done = False
                    Incr i
                Loop
             
                Print "i=",i,"j=",j
            End Function
             
             
            #include <stdio.h>
             
            main() 
            {
                int i=0,j;
                bool done=false;
             
                while ( i<10 && !done )
                {
                    printf("i=%d\n",i);
                    j=0;
                    while (j<10 && !done);
                    {
                        printf("j=%d\n",j);
                        if (j==5) 
                        {
                            done = true;
                        }
                        j++;        
                    } 
                    i++;
                }
                printf("i=%d\tj=%d\tdone=%d\n",i,j);
            }

            Comment


            • #7
              Originally posted by Andre Smit:
              Which proves my point I'll leave it at that.
              You actually have no point. Use what the language makes available to you.

              And as for the "goto" - surely you'd save that for some seriously inner loop exit.

              Comment


              • #8
                Originally posted by Andre Smit:
                Marco - some code snippets to illustrate my point.
                OK, I understand.
                But, as Ron showed, that's nothing wrong using GoTo when it's needed / suitable.
                In some situation, it's just the simpler, clearer and fastest solution, IMHO.

                Bye!

                ------------------
                Try Online TrID file identifier! Recognize over 1.800 filetypes and counting...
                Give a powerfull scriptable Lua interface to your application with PowerBLua
                Leakout - Firewall outbound control tester

                [This message has been edited by Marco Pontello (edited February 12, 2006).]
                -- The universe tends toward maximum irony. Don't push it.

                File Extension Seeker - Metasearch engine for file extensions / file types
                Online TrID file identifier | TrIDLib - Identify thousands of file formats

                Comment

                Working...
                X