techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Oct 28, 2010, 12:38 PM   #1
Seany1212
200 Posts
 
Seany1212's Avatar
 
Join Date: Jul 2006
Location: England, UK
Posts: 443 (0.18/day)
Thanks: 18
Thanked 13 Times in 11 Posts

System Specs

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
Seany1212 is offline  
Reply With Quote
Old Oct 28, 2010, 12:55 PM   #2
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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
Kreij is offline  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Oct 28, 2010, 01:32 PM   #3
streetfighter 2
1000 Posts
 
streetfighter 2's Avatar
 
Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts

System Specs

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.
   }
}
Also, couldn't you use a switch-case statement for that instead of if-else? Case statements are usually more efficient.
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!");
I think some versions of C# don't have fall through case statements so that might not work.

Last edited by streetfighter 2; Oct 28, 2010 at 02:09 PM.
streetfighter 2 is offline  
Reply With Quote
The Following User Says Thank You to streetfighter 2 For This Useful Post:
Old Oct 28, 2010, 03:34 PM   #4
Seany1212
200 Posts
 
Seany1212's Avatar
 
Join Date: Jul 2006
Location: England, UK
Posts: 443 (0.18/day)
Thanks: 18
Thanked 13 Times in 11 Posts

System Specs

Quote:
Originally Posted by Kreij View Post
Use a while statement.
Assume "X" is your exit condition.
Code:
While (string != "X")
{
    Do something
}
This worked wonders, thanks kreij . 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
Seany1212 is offline  
Reply With Quote
Old Oct 28, 2010, 03:38 PM   #5
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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
Kreij is offline  
Reply With Quote
Old Oct 28, 2010, 08:45 PM   #6
streetfighter 2
1000 Posts
 
streetfighter 2's Avatar
 
Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts

System Specs

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);
Notice how the first line of the code declares the string (initialized to 'null') which makes it available outside of the while loop and switch statement?

Hope that helps with any future C# coding you do.
streetfighter 2 is offline  
Reply With Quote
Old Oct 28, 2010, 08:52 PM   #7
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

@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
Kreij is offline  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Oct 28, 2010, 09:30 PM   #8
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

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(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Oct 28, 2010, 09:57 PM   #9
streetfighter 2
1000 Posts
 
streetfighter 2's Avatar
 
Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts

System Specs

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...
streetfighter 2 is offline  
Reply With Quote
Old Oct 28, 2010, 10:41 PM   #10
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

No real reason to use ..
Code:
if (exit) break;
since you can just use the exit boolean as the while condition ...
Code:
while (!exit)
{
    ... one of the cases sets exit to true;
}
But that's the beauty of coding. Many ways to do the same thing.

Quote:
Originally Posted by Street
Also, I'm pretty damned certain I'm over-analyzing this...
We're coders, it's in our nature.
__________________

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
Kreij is offline  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Oct 29, 2010, 01:14 AM   #11
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

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(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 07:02 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts