while loop

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1212 Views - Last Post: 17 March 2011 - 08:23 AM Rate Topic: -----

#1 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 32
  • Joined: 13-March 11

while loop

Posted 16 March 2011 - 07:53 PM

hi there i am doing this exercise which is not a homework but rather something i am trying to do for the sake of it

can anyone tell me what is wrong with this because it looks perfect to me but still its not working when i try to run the program

 int number;
            textBox1.Clear();
                number = 2;

            while(number <10);

            textBox1.AppendText(Convert.ToString(number)  + "  ");
                number++;

This post has been edited by modi123_1: 16 March 2011 - 08:07 PM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: while loop

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9258
  • View blog
  • Posts: 34,735
  • Joined: 12-June 08

Re: while loop

Posted 16 March 2011 - 08:09 PM

Can you explain "still its not working" means? You have a while loop that has no body.

Are you trying to do something like this?
while(number <10)
{

textBox1.AppendText(Convert.ToString(number)  + "  ");
number++;
}

Was This Post Helpful? 0
  • +
  • -

#3 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 32
  • Joined: 13-March 11

Re: while loop

Posted 16 March 2011 - 08:16 PM

yes it has the boy like you pointed out

i meant it has the body

this is exactly what it looks like

 private void button1_Click(object sender, EventArgs e)
        {
            int number;
            textBox1.Clear();
                number = 2;

            while(number <10);

            textBox1.AppendText(Convert.ToString(number)  + "  ");
                number++;


        }




This post has been edited by modi123_1: 16 March 2011 - 08:47 PM
Reason for edit:: seriously - use the code tags.

Was This Post Helpful? 0
  • +
  • -

#4 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: while loop

Posted 16 March 2011 - 08:33 PM

As modi123_1 pointed out
No body

Remove semi-colon after

while(number <10);
Was This Post Helpful? 2
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9258
  • View blog
  • Posts: 34,735
  • Joined: 12-June 08

Re: while loop

Posted 16 March 2011 - 08:48 PM

No trigger - you have no body for your while loop. Check out what I said.
Was This Post Helpful? 0
  • +
  • -

#6 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 32
  • Joined: 13-March 11

Re: while loop

Posted 16 March 2011 - 08:54 PM

thanks man it partly works now like it displays one number all the way through. say u put numer = 3
then while(number <= 20 )
then it will display 3 everywhere and wont stop

but thanks one step closer now lol
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: while loop

Posted 16 March 2011 - 09:03 PM

I suspect you have the increment (number++) outside the while body.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9258
  • View blog
  • Posts: 34,735
  • Joined: 12-June 08

Re: while loop

Posted 16 March 2011 - 09:05 PM

Did you put a bracket underneath the while statement and a close bracket after the "number++;"? I am certain you didn't because the moment you removed that semi colon on the while line it thinks the next line is it's body..which is appending the number. You need to extend the scope with the brackets... as I said.
Was This Post Helpful? 0
  • +
  • -

#9 trigger202  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 32
  • Joined: 13-March 11

Re: while loop

Posted 16 March 2011 - 09:34 PM

thanks for the response people it turns out i was putting the number++ outside brackets

before
textBox1.Appendtext(convert.ToString(number) + "")
number++;

after
textBox1.Appendtext(convert.ToString(number) + ""+ number++)

but whats doing is displaying twice each number
1122334455667788991010
Was This Post Helpful? 0
  • +
  • -

#10 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: while loop

Posted 16 March 2011 - 11:33 PM

that's because you told it to print it twice (you add the Convert and the number itself). You need it on different lines:
while (number <=10) {
    textBox1.Appendtext(number.ToString());
    number++;
}

Notice the brackets around the code that belongs to the while. Get used to using them.
Was This Post Helpful? 0
  • +
  • -

#11 Theomi  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 88
  • Joined: 17-January 10

Re: while loop

Posted 17 March 2011 - 01:21 AM

View Posttrigger202, on 16 March 2011 - 09:34 PM, said:

thanks for the response people it turns out i was putting the number++ outside brackets

before
textBox1.Appendtext(convert.ToString(number) + "")
number++;

after
textBox1.Appendtext(convert.ToString(number) + ""+ number++)

but whats doing is displaying twice each number
1122334455667788991010


This is brackets { }

This is parentheses ( )
Was This Post Helpful? 1
  • +
  • -

#12 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: while loop

Posted 17 March 2011 - 03:15 AM

Please follow modi123_1 Suggestion on how to use brackets.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: while loop

Posted 17 March 2011 - 04:41 AM

View PostTheomi, on 17 March 2011 - 02:21 AM, said:

This is brackets { }

This is parentheses ( )


Not quite

Brackets are square: [ ]
Braces are curly: { }
Parentheses are curved: ( )
Was This Post Helpful? 2
  • +
  • -

#14 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: while loop

Posted 17 March 2011 - 05:13 AM

Cleaned up code should probably be closer to this. I hope it helps you wrap your head around a while loop. But really, just looking this up in the MSDN would have saved you a ton of aggravation. Whether it is code syntax now while you are learning, or the SDK guidelines of a third party piece of hardware a year from now; you have to accept that you will be looking up instructions in the documentation for as long as you are coding.

                textBox1.Clear();
                int number = 2;

                while (number < 10)
                {
                    textBox1.AppendText(number++.ToString());
                }


Was This Post Helpful? 0
  • +
  • -

#15 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: while loop

Posted 17 March 2011 - 07:23 AM

Quote

Brackets are square: [ ]
Braces are curly: { }
Parentheses are curved: ( )


Actually they are called curly bracket's , and not braces. ;)

This post has been edited by marinus: 17 March 2011 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2