SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

intro & for (foreach) statement - is there some magic needed?

Last post 09-11-2007 12:43 AM by davegator83. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 09-10-2007 4:47 AM

    intro & for (foreach) statement - is there some magic needed?

    Hello,

    I am relatively new to c# and sharp develop.  I have done most of my recent work with MS Access (VBA) and Visual Basic.  I am working on a database application at work and am trying to redo what I have done so far in c#.  I am having some trouble with a simple "for" statement.  For example, the following block of code (the part inside the {}) is skipped.

    for (int k=0;k==5;k++)

    {ktemp = k;}

    ... 

    This is not what my app needs, but I have simplified it to diagnose what is going on.

     However, the following while loop works fine

    int i=0;

    int j=0;

    int itemp;

    int jtemp;

    int ktemp;

    while (i <= 5)

           {while (j <= 5)

                  {itemp = i;

                   jtemp = j;

                   j++;

                  }

            i++;

            }

     By the way, I get the same result with MS C# Express 2005 as with SharpDevelop (also same results with a foreach loop).  I would appreciate any help or suggestions as to what I am doing wrong.

    Thanks,

    Dave in Colorado

  • 09-10-2007 9:54 AM In reply to

    Re: intro & for (foreach) statement - is there some magic needed?

    Hello Dave,

    do I understand your problem correctly: you don't know how to change while into for? If so:

    Your main problem seems to know how for works:

    1. The first statement in for-instruction initializes the loop variable.
    2. The last statement rules how to finish a loop.
    3. The middle statement controls if a loop has to be started.
    4. Each of these statements can be empty (or extended to more than a single instruction).

    Under these rules it works as follows:

    1. Starting the for-instruction, the initialization, i.e. the first statement, is done.
    2. The middle instruction is done, i.e.the framework controls whether the condition is fulfilled to do the first loop.
    3. The first loop is done.
    4. The final instruction, i.e. the last statement, is done.
    5. The middle instruction is done, i.e. the framework controls whether the condition is fulfilled to redo the loop.
    6. And so on...

    You can change your while-instructions in this way:

        for ( int i = 0; i <= 5; i++) {
            for ( int j = 0; j <= 5; j++ ) {
                itemp = i;
                jtemp = j;
            }
        }

    I hope this makes it a little bit clearer. Juergen

    By the way, this is not a problem on #D, but on the language C# itself. 

    Software fuer Verlage (Publishers)
    #D + NET 2.0 + Firebird 2.0
    (formerly Delphi 5 + 2005 Pro)
  • 09-10-2007 3:03 PM In reply to

    Re: intro & for (foreach) statement - is there some magic needed?

    Juergen,

    Thnk you for your kind reply to what I am sure seems like a pretty stupid question.  Actually, I was not trying to convert the while statements but rather trying to get the for statements to work at all. When I set a breakpoint and stepped through the code, the inner part of the for statement gets bypassed.  I wote the while statement to see if I could at least get that to work.  I will try your example.  

     Thanks,

     

    Dave in Colorado
     

  • 09-10-2007 5:26 PM In reply to

    Re: intro & for (foreach) statement - is there some magic needed?

    davegator83:

    When I set a breakpoint and stepped through the code, the inner part of the for statement gets bypassed.

    StepBy F11. Juergen 

    Software fuer Verlage (Publishers)
    #D + NET 2.0 + Firebird 2.0
    (formerly Delphi 5 + 2005 Pro)
  • 09-11-2007 12:43 AM In reply to

    Re: intro & for (foreach) statement - is there some magic needed?

    Juergen,

    Well, I said that my question was stupid but I didn't think it was THAT stupid. Now I know better!  I made the mistake of having the conditional part of the for statement be the FINAL condition not the initial one.  When I changed it from i=0;i==5;i++ to i=0;i<=5;i++ it runs fine. 

     Now I know better.  Thanks for your help.

    Dave in Colorado

Page 1 of 1 (5 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.