-2

Im trying to learn loops and they are on the verge of breaking me. This loop has either not worked at all or never ended which has lead to some serious stress. If anyone could help me out it would be great. The plan is for the loop to continue until someone writes yes/Yes/YES (any form of yes preferably) and then break and continue the next part of the code which is some readlines and write lines, haven't gotten that far because the loop hasn't let me yet. Very thankful for any input.

        Console.WriteLine("Hello Inspector!");
        Console.WriteLine("Are you ready to identify the suspects? Just write yes and we'll get started.");
        string Start = Console.ReadLine();

        Startup();

        while (Start.Contains("")==false)
        {
            Console.WriteLine("Just type yes when you are ready.");

            if (Start.Contains("Yes") == true)
                Console.WriteLine("Let's start.");
            break;
        }
    }

    static void Startup()
    {
        string Start = Console.ReadLine();

        if (Start.Contains("yes") == true)
        {
            Console.Clear();
            Console.WriteLine("Here are the suspects:");
        }

        else
        {
            Console.WriteLine("Just type yes when you are ready.");
        }
    }
}

}

  • What is supposed to mean _Start.Contains("")_ ? – Steve Mar 16 '20 at 21:04
  • 2
    You are not updating your `Start` variable inside your loop, that's why it never ends. – Chronicle Mar 16 '20 at 21:10
  • 2
    The `break` outside the `if` will execute on the first loop, so it's like there was no loop at all. If I understand the intent of the code, the `break` should be inside the `if`, which means you have to add curly braces. Also, the `Startup` and the `while` loop seem to be doing the same thing. Since you're learning loops, I would suggest to remove it it and only focus on the `while` loop approach. – Marko Gresak Mar 16 '20 at 21:11
  • 1
    Pro tip: as .Contains has a bool result, you don't need to compare it with true – Hans Kesting Mar 16 '20 at 21:20
  • @MarkoGrešak yes that could be a smart thing to leave the break but just because I'm very curious, where should i put the break; for it to be included in the loop. – Julia Johansson Mar 16 '20 at 21:21
  • 1
    The "Start=Console.ReadLine()" line just executes once, it doesn't associate the Start variable with anything you type in afterwards, if that is what you think – Hans Kesting Mar 16 '20 at 21:22
  • 1
    Contains with an empty string always returns true. You want `Start ==""` or use String.IsNullOrEmpty(Start) – Hans Kesting Mar 16 '20 at 21:26
  • Hello, Julia, and welcome to Stack overflow. As a minor note, a best practice is to always use `{` braces `}` around code blocks—such as within `if…` statements—as it ensures that the contained logic is _explicit_. Otherwise it's easy to have code that _looks_ right, but doesn't behave as expected. In this case, as @MarkoGrešak notes, without the `{` braces `}`, only the next statement (i.e., up to the subsequent `;`) is conditionally executed, but the subsequent `break;` is _always_ executed. – Jeremy Caney Mar 16 '20 at 23:30

1 Answers1

2

There are several issues with your code:

1) You only once read user input - as m.sh already noted, you need to put

Start = Console.ReadLine();

inside your while loop.

2) Your break you expect only to catch if your condition is met is outside the scope because you are missing enclosing { } like this:

if (Start.Contains("Yes") == true)
{
    Console.WriteLine("Let's start.");
    break;
}

3) Not directly a programming bug but widely frowned upon: explicitly comparing boolean. Simply use

if (Start.Contains("yes"))

instead of

if (Start.Contains("yes") == true)

4) Also already mentioned - use .ToLower() to allow any input casing

if (Start.ToLower().Contains("yes"))

will work for yes, YES, yEs, YeS, ...

Putting together the parts for a working loop

// many coding guidelines ask you to use string.Empty rather than "". [I.]
string Start = string.Empty; 

while (!Start.ToLower().Contains("yes"))
{
    Console.WriteLine("Just type yes when you are ready.");
    Start = Console.ReadLine();
}

Console.WriteLine("Let's start.");

Note the negation ! for the while condition - this makes your loop run as long as the condition is not met instead of having to check inside your loop if you need to break out.

Another way to loop could be do { } while(); where your condition is checked at the end of the loop:

string Start = string.Empty;

do
{
    Console.WriteLine("Just type yes when you are ready.");
    Start = Console.ReadLine();
}
while (!Start.ToLower().Contains("yes"));

If you step through your code running in debugger, you will notice the different behavior and how do {} while() can be considered faster code than while() { }.


I. In C#, should I use string.Empty or String.Empty or “” to intitialize a string?

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Thank you so much for the very in-depth information. I'll make sure to use this answer in the future as well because it really helped to clear things up! :) – Julia Johansson Mar 16 '20 at 22:54