Computer Science Canada

C# For loop help

Author:  nemisis652 [ Wed Dec 09, 2009 8:03 pm ]
Post subject:  C# For loop help

Hey, I wasn't quite sure if I should be leaving this here, if this belongs in another board just let me know and I'll take it elsewhere. I am in a software fundamentals class and we are learning C#, and I'm having little issues with my final assignment.

The assignment is to create a For loop that takes a users Starting Number, Ending Number, and Number they wish to count by, then its supposed to count up to that number in a list box. It seemed really simple when I got it but now that I am into the code, I have no idea what to do! This is my first foray into the For loop in any language, and I'm almost certain I am brainfarting here, but if anyone could give me some pointers on using the For loop and what types of things I would need to complete this assignment I would greatly appreciate it.

Heres some of what I have so far:

C#:

        private void btnStart_Click(object sender, EventArgs e)
        {
            //declarations
            int intStartNum, intEndNum, intCountRate, counter;

            //Project Mainline
            //gather required data
            try
            {
                getData(out intStartNum, out intEndNum, out intCountRate);
            }
            catch
            {
                MessageBox.Show("Input is non numeric or invalid", "Input Error");
                txtStartNum.Focus();
                return;
            }

            //Clear List Box
            lstOutput.Items.Clear();

            //Output

//This is where I start to draw a blank of what I am supposed to do
            for (counter = 0; )
            {


        }

        private void getData(out int getStartNum, out int getEndNum, out int getCountRate)
        {
            getStartNum = int.Parse(txtStartNum.Text);
            getEndNum = int.Parse(txtEndNum.Text);
            getCountRate = int.Parse(txtCountRate.Text);
        }




P.s. I'm pretty new to this site so I am also not sure how to embed code into my post, sorry :S

Mod edit: You should look into the [ syntax="csharp" ] [ /syntax ] tags.

Author:  rdrake [ Wed Dec 09, 2009 8:18 pm ]
Post subject:  Re: C# For loop help

Something like
C#:
for (int <name> = <someval>; <name> <condition> <someotherval>; <increment name>) { }


So typically
C#:
for (int i = 0; i < 42; i++) { }


Of course you can also increment i by some number other than 1.
C#:
for (int i = 0; i < 42; i += 2) { }

Author:  Tony [ Wed Dec 09, 2009 8:23 pm ]
Post subject:  Re: C# For loop help

nemisis652 @ Wed Dec 09, 2009 8:03 pm wrote:
I am in a software fundamentals class

Sounds like something that might be taught beyond high school.

Quote:
and I'm having little issues with my first assignment.

FTFY

http://msdn.microsoft.com/en-us/library/ch45axte.aspx

Author:  nemisis652 [ Wed Dec 09, 2009 8:24 pm ]
Post subject:  Re: C# For loop help

so what your saying is i could possibly:

C#:
for (counter = intStartNum; counter < intEndNum; counter+= intCountRate)


and this would allow my loop to start at the number the user inputted, increment by the users rate and then stop before reaching the users ending number?

Mod Edit: You were supposed to strip out the spaces. I put them in there so you could actually see the tags.

Author:  nemisis652 [ Wed Dec 09, 2009 8:27 pm ]
Post subject:  Re: C# For loop help

also sorry about the tags thing, i just kinda found how to do it, ill revise:

C#:


for (counter = intStartNum; counter < intEndNum; counter+= intCountRate)


Author:  rdrake [ Wed Dec 09, 2009 8:28 pm ]
Post subject:  RE:C# For loop help

You can edit your posts. And yes, that looks like it's what you want.

Author:  nemisis652 [ Wed Dec 09, 2009 8:43 pm ]
Post subject:  Re: C# For loop help

Thanks for the help guys, also thank you for that Link to the reference. It helped a lot, hopefully I can get this program working in time.

Take it easy friends


: