Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Looping in C#

Options
  • 16-10-2011 7:54pm
    #1
    Registered Users Posts: 1,115 ✭✭✭


    Right so got my first proper assignment for college today and I have a problem with trying to loop.

    I have a section of 60 lines of code, which runs off 4 set variables and 1 variable that needs to change a few times to create a table of data.

    (Brackets are writing in subscript) My variable, D, is controlled by the equation
    D(i) = 2D(i -1)
    
    , starting at D(1) = 0.02 and (i) increasing from 1 to 10.

    Ive only been shown for loops only and have no idea how to do this :S

    Anyone have any ideas how I repeat the 60 lines for the doubling value of D as shown above?


Comments

  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    magicianz wrote: »
    Right so got my first proper assignment for college today and I have a problem with trying to loop.

    I have a section of 60 lines of code, which runs off 4 set variables and 1 variable that needs to change a few times to create a table of data.

    (Brackets are writing in subscript) My variable, D, is controlled by the equation
    D(i) = 2D(i -1)
    
    , starting at D(1) = 0.02 and (i) increasing from 1 to 10.

    Ive only been shown for loops only and have no idea how to do this :S

    Anyone have any ideas how I repeat the 60 lines for the doubling value of D as shown above?

    I'm not 100% sure what you are looking for, but it seems a for loop is what you need.

    You seem to be a newbie so I'll explain a for loop.
    for( StatementA; StatementB; StatementC){
         //Do stuff
    }
    

    StatementA gets executed before the loop begins. Typically used for setting up variables that the loop will use.
    StatementB gets checked before each loop, if its value is TRUE, the code between the 2 {} will execute.
    StatementC gets executed after the code between the 2 {} get executed.

    Execution flows something like this
    1. StatementA
    2. StatementB is TRUE
    3. Do Stuff
    4. Statement C
    5. StatementB is TRUE
    6. Do Stuff
    7. Statement C
    8. StatementB is FALSE
    9. End Loop

    So for your problem, as I understand it, you want to execute
    D(i) = 2D(i -1)
    
    with i holding the values 1 - 10
    for(int i = 1; i <= 10; i = i + 1)
    {
       D(i) = 2D(i -1);
    }
    

    From here on is extra stuff that's handy to know.

    Since you are new to C#, I incremented i the long way, there are 2 shorter ways in C languages.
    To add a given number (call it j) to i you would say
    i += j;
    
    Or to increase i by one.
    i++;
    
    You can decrement the same way.
    i -= j;
    i--;
    

    Keep in mind, anything you declare from the 'for' to the '}' will be local to the for loop, you wont be able to access the variables outside the loop. Also, since StatementB and C should get executed more than one, declaring variables in those statements will lead to errors.
    int visableToAll = 0;
    for(int visableToFor = 0, visableToAll = 10; visableToAll > visableToFor; visableToFor++)
    {
        console.writeLine(visableToFor);
        console.writeLine(visableToAll);
    }
    
        console.writeLine(visableToFor);//COMPILE ERROR, Variable out of scope
        console.writeLine(visableToAll);//this one is fine
    
    You can see in my StatementA that I put 2 operations in there instead of one, putting a ',' between operations lets you put as many as you like in there. The same holds for StatementC.

    If you don't want to do anything in some of the statements, leave them blank.
    for(;;i++)
    
    Leaving the middle one blank should cause the loop to go forever, and that's typically bad.

    Any questions at all, feel free to ask.


  • Registered Users Posts: 1,115 ✭✭✭magicianz


    I tried the for loop before for this but myself and all my friends keep getting the same error - " 'D' is a 'variable' but it is used like a 'method'. " =/

    For the loop, I have 4 set variables that I need to use in it (g, Pf, Pp and Mew), and then in the block of code, it solves for 2 more variables (u and Re). So from what you're saying, I have to declare my 4 set variables in "StatementA"?

    What about the 2 variables that are calculated in the block of code itself? :o

    Also, for calculating my D, it needs to be doubled each time the loop runs, so do I have to put a statement in "StatementA" part that does that?

    The only equation that I can think of that will accomplish that for incrementing values of i is ( Where c is just a random variable for the equation )
    c = D * math.pow(2, i)
    

    So that when i is increased by 1, D is doubled?

    Im sorry if this is really unclear, not used to this stuff properly at all >.<

    EDIT: Yeah it all seems so clear now :P Have it working and all there perfectly now, thanks Genghiz =D


Advertisement