![]() |
|
|
#1 |
![]() Join Date: Jul 2006
Location: England, UK
Posts: 443 (0.18/day)
Thanks: 18
Thanked 13 Times in 11 Posts
|
C# looped if statements
Hi folks, ive just begun programming and im having trouble with looping the result of an if else statement, basically my code looks like this:
Console.Clear(); Console.Write("Please enter your catagory: "); string = Console.ReadLine(); if (string == "A" || string == "a"); { Console.Write("You entered A"); } else if (string == "B" || string == "b"); { Console.Write("You entered B"); } else if (string == "C" || string == "c"); { Console.Write("You entered C"); } else { Console.Write("You have not entered a valid catagory"); Console.Write("\nPress enter to re-enter..."); } I tried entering a do while but in the while condition i didnt know what to put so that it loops the result of the else, i want it to loop from the clear so that the person can re-enter there catagory if the else statement is activated. Thanks for any help/advice
__________________
“The Earth will heat up like a mofo, then cool down to a point where you would be reaching 10GHz overclocks on stock air cooling... at the equator.” -hat
Increase the population of TPU City: http://tpu.myminicity.com |
|
|
|
|
|
#2 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Use a while statement.
Assume "X" is your exit condition. Code:
While (string != "X")
{
Do something
}
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#3 |
![]() Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts
|
He could also use a break statement to do what he wants. (Though it doesn't really matter...)
Code:
While (1)
{
Console.Clear();
Console.Write("Please enter your catagory: ");
string = Console.ReadLine();
if (string == "A" || string == "a");
{
Console.Write("You entered A");
break;
}
else if (string == "B" || string == "b");
{
Console.Write("You entered B");
break;
}
else if (string == "C" || string == "c");
{
Console.Write("You entered C");
break;
}
else
{
Console.Write("You have not entered a valid catagory");
Console.Write("\nPress enter to re-enter...");
Console.ReadLine(); //If you forget this you'll never see the text in this else condition.
}
}
http://msdn.microsoft.com/en-us/libr...7t(VS.80).aspx You can use "string.ToUpper()" so you don't need to worry about whether it's upper or lower case. I'd probably write it more like this: Code:
While (1)
{
Console.Clear();
Console.Write("Please enter your catagory: ");
string = Console.ReadLine();
string = string.ToUpper;
switch (string)
{
case "A":
case "B":
case "C":
Console.WriteLine("You entered {0}",string);
goto End;
default:
Console.Write("You have not entered a valid catagory");
Console.Write("\nPress enter to re-enter...");
Console.ReadLine();
break;
}
}
End:
Console.Write("Success!");
__________________
Last edited by streetfighter 2; Oct 28, 2010 at 02:09 PM. |
|
|
|
| The Following User Says Thank You to streetfighter 2 For This Useful Post: |
|
|
#4 | |
![]() Join Date: Jul 2006
Location: England, UK
Posts: 443 (0.18/day)
Thanks: 18
Thanked 13 Times in 11 Posts
|
Quote:
. Tried the switch streetfighter but couldnt get it to lead onto the rest of the application because of strings i was creating in the cases then trying to expand on outside of the switch, thats probably because i dont know much about switches though
__________________
“The Earth will heat up like a mofo, then cool down to a point where you would be reaching 10GHz overclocks on stock air cooling... at the equator.” -hat
Increase the population of TPU City: http://tpu.myminicity.com |
|
|
|
|
|
|
#5 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Describe what you are trying to do in a little more detail.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#6 |
![]() Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts
|
I think I know what he's talking about.
The variables you want to work with outside of the switch/while must be declared outside of the switch/while. (I believe this concept is referred to as the "scope" of a "local variable"). For example: Code:
string string = null;
While (1)
{
Console.Clear();
Console.Write("Please enter your catagory: ");
string = Console.ReadLine();
string = string.ToUpper;
switch (string)
{
case "A":
case "B":
case "C":
Console.WriteLine("You entered {0}",string);
goto End;
default:
Console.Write("You have not entered a valid catagory");
Console.Write("\nPress enter to re-enter...");
Console.ReadLine();
break;
}
}
End:
Console.WriteLine("I HAS YER STRNG: {0}",string);
Hope that helps with any future C# coding you do.
__________________
|
|
|
|
|
|
#7 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
@Seany : Quit tip ... don't use a goto. Ever.
The battle between people who use goto's and those who do not, has been raging for years. It's a lot like the apple/PC or Nvidia/ATI battles. Choose your side now ... and choose wisely.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#8 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts
|
I only use goto statement in batch files (because there really is no alternative). In the above example, I would have done this:
Code:
char key;
bool exit = false;
while (true)
{
Console.Clear();
Console.Write("Please enter your catagory: ");
key = Console.ReadKey();
Console.WriteLine();
switch (key)
{
case 'a':
case 'b':
case 'c':
Console.WriteLine("You entered " + key.ToString());
exit = true;
break;
default:
Console.WriteLine("You have not entered a valid catagory");
Console.Write("Press enter to re-enter...");
key = Console.ReadKey();
break;
}
if (exit)
break;
}
Console.WriteLine()
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
| The Following User Says Thank You to FordGT90Concept For This Useful Post: |
|
|
#9 |
![]() Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts
|
Yeah, I would have assigned an exit bool as well but I can't even remember the computational issues (branch prediction) of goto statements in C#. It seemed to me that the "goto" was just simpler and given that the script itself is about as simple as it gets I assumed it was trivial.
The other option to avoid having to make a flag variable is to make a function with while/switch statement and then have "return;" instead of "goto" or "break;" inside the case statement. I'm not sure if the overhead on functions in C# makes this method worthwhile though... Also, I'm pretty damned certain I'm over-analyzing this...
__________________
|
|
|
|
|
|
#10 | |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
No real reason to use ..
Code:
if (exit) break; Code:
while (!exit)
{
... one of the cases sets exit to true;
}
Quote:
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#11 |
|
"I go fast!1!11!1!"
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts
|
The way I wrote it is more versatile because you can stop the loop before it reaches the conditional (like if there was more code under the if statement). Since I've had to do that some times, I always do it the same way.
Another advantage to not using goto is that you can make sure all streams are closed before exiting the loop (assuming the string is defined and opened inside).
__________________
Golden Rule of Programming: Never assume. try { SteamDownload(); } catch (Steamception ex) { RageQuit(); } |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem using WMI statements (C# code) | MrSeanKon | Programming & Webmastering | 3 | May 29, 2007 03:30 PM |