0

I was wondering if someone can help me. I am very new to programming and this is my first time using stackoverflow.

I am looking at creating a while loop that ask the user for a their background and once the input is correct to break out of the loop. I could use break to exit out but I was trying to find another way. My code so far is:

var background = "";

while (background != "M" || background != "W" || background != "R")
{ 
    Console.WriteLine("Welcome " + name + ", " + "Please pick a class: \n" +
                              "(M)age \n" +
                              "(W)arrior \n" +
                              "(R)ogue \n");
    var readLine = Console.ReadLine();
    if (readLine != null) background = readLine.ToUpper();

    if (background == "M")
    {
        Console.WriteLine("Welcome Mage " + name);            
    }
    else if (background == "W")
    {
        Console.WriteLine("Welcome Warrior " + name);                    
    }
    else if (background == "R")
    {
        Console.WriteLine("Welcome Rogue " + name);
    }
    else
    {
        Console.WriteLine("Invalid choice"); 
    }
}
4
  • 1
    What's wrong with break? Commented Jun 12, 2017 at 19:27
  • 1
    @JJJ - in general, control structures with one entry and one exit are easier to understand, related to the single entry/single exit rule. Commented Jun 12, 2017 at 19:38
  • It just seem wrong to me when I made a condition which should of stopped the loop and I still had to use a break to get out it. What was the point in having the condition in the first place. Ok my condition was wrong and that why I was stuck. Commented Jun 12, 2017 at 19:52
  • Since you already have an accepted answer, I'll just mention that this seems like a natural fit for a do/while loop to me. Commented Jun 12, 2017 at 20:05

3 Answers 3

5

"||" is "or". Your while expression will always be true, because it is always true that background is not "M", or it is not "W", or it is not "R".

With "or", all three of the checks there must be false for the whole expression to be false, but at least two of those "!=" subexpressions must always be true at any time. If background is "M", it can't be "W" or "R".

    while (background != "M" || background != "W" || background != "R")

What you want is "&&", meaning "and": You want to continue the loop while background is not "M" and background is not "W", etc.

    while (background != "M" && background != "W" && background != "R")
Sign up to request clarification or add additional context in comments.

3 Comments

That make's sense. Thank you very much.
@SerbjitKang if this answered your question please make sure you make this the accepted answer
@SerbjitKang My pleasure. Tyler Gaffaney's way of writing the loop is a good idea too, but I thought it would be helpful to understand where yours went wrong.
0

Using a 'flag' to specify if the loop should break or not will work in your case.

var background = "";
bool breakLoop = false;

    while (!breakLoop)
    { 
        Console.WriteLine("Welcome " + name + ", " + "Please pick a class: \n" +
                          "(M)age \n" +
                          "(W)arrior \n" +
                          "(R)ogue \n");
        var readLine = Console.ReadLine();
        if (readLine != null) background = readLine.ToUpper();

        if (background == "M")
        {
            Console.WriteLine("Welcome Mage " + name);
            breakLoop = true;
        }

        else if (background == "W")
        {
            Console.WriteLine("Welcome Warrior " + name);
            breakLoop = true;
        }

        else if (background == "R")
        {
            Console.WriteLine("Welcome Rogue " + name);
            breakLoop = true;
        }

        else
            Console.WriteLine("Invalid choice"); 
    }

Comments

0

I personally like to create a list of accepted responses and check if the user input is in that list.

var name = "Bob";
var validTypes = new List<string> {"M", "W", "R"};
while (true)
{
    Console.WriteLine($"Welcome {name}, please pick a class" +
                       "\n(M)age" +
                       "\n(W)arrior" +
                       "\n(R)ogue" +
                       "\n");
    var charType = Console.ReadLine();

    if (validTypes.Contains(charType.ToUpper()))
    {
        break;
    }
    else
    {
        Console.WriteLine("Please enter a valid class letter");
    }

1 Comment

Note that this snippet does not take into account any exception handling should a user input something that can't be made ToUpper(), it's just for example purposes of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.