Do while loop in C# [SOLVED]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Do while loop in C# [SOLVED]

Post by mattykins »

I ask you guys way too many noob questions haha i'm sorry.

So i'm writing a do while loop as a test and my code thus far looks like this:

Code: Select all

           do
            {
                string userinput;
                userinput = Convert.ToString(Console.ReadLine());
            }
            while (userinput != "escape");
And I get the error 'userinput' does not exist in the current context.

Is it because i declared userinput inside of the loop?

Thanks in advanced :)
Last edited by mattykins on Fri Jan 20, 2012 6:02 am, edited 1 time in total.
User avatar
Zak Zillion
Posts: 112
Joined: Thu Apr 07, 2011 12:55 am

Re: Do while loop in C#

Post by Zak Zillion »

I've been learning java and not C# lol but I think its because userinput is considered a local variable, for you to be able to use it in the while statement you'd have to make it a class variable or something :P I hope that helps!
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - Albert Einstein

Old Project(s):
The Dark Flame
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Do while loop in C#

Post by mattykins »

Thanks! But how would i go about doing that?
User avatar
Zak Zillion
Posts: 112
Joined: Thu Apr 07, 2011 12:55 am

Re: Do while loop in C#

Post by Zak Zillion »

Code: Select all

do
            {
                string userinput;
                userinput = Convert.ToString(Console.ReadLine());
                return userinput;
            }
            while (userinput != "escape");
I think this is how you do it! Hopefully it's something like java. :P All this does is return the local variable to the class.
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - Albert Einstein

Old Project(s):
The Dark Flame
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Do while loop in C#

Post by mattykins »

I'll try that right now :) Thanks. I'm still pretty noob here heh.
User avatar
Zak Zillion
Posts: 112
Joined: Thu Apr 07, 2011 12:55 am

Re: Do while loop in C#

Post by Zak Zillion »

Good luck! I am too. :P
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - Albert Einstein

Old Project(s):
The Dark Flame
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Do while loop in C#

Post by mattykins »

Sorry for double post! But its giving me the same error even with return userinput;
User avatar
Zak Zillion
Posts: 112
Joined: Thu Apr 07, 2011 12:55 am

Re: Do while loop in C#

Post by Zak Zillion »

Well this is the point were I hand it off to the more experienced devs. ;)
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - Albert Einstein

Old Project(s):
The Dark Flame
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Do while loop in C#

Post by Jackolantern »

You can't return a variable from a FOR loop. Only from methods. Set it up like this:

Code: Select all

   string userinput = "";
   do
   {                
       userinput = Convert.ToString(Console.ReadLine());
   }
   while (userinput != "escape");
That way the userinput variable will be available both inside and outside of the DO-WHILE loop.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: Do while loop in C#

Post by mattykins »

Awesome man :) Thanks.
Post Reply

Return to “Beginner Help and Support”