How do i exit this loop inside a loop
I have a looped menu but the loop in one loop is an infinate loop, what should i do?
3 Replies
Hello, I undestand you infinite loop is the do-while() loop. In order to break the parent loop you can either break it or have a condition to break it i.e Console.ReadKey() == ConsoleKey.Escape or a CancellationToken.
Anyway there is missing information and context regarding what are you trying to do. Please give me more info so I could guide you
that was an older version and what i meant is when im in the inner while loop
and even if the the condition hits it turns black and dosent go back to the first do loop
but i did something different
Console.WriteLine("choose an option");
Console.WriteLine("Start(1)");
Console.WriteLine("help(2)");
Console.WriteLine("exit(3)");
string userInput = Console.ReadLine();
bool finished = false;
bool done = false;
do
{
if (userInput == "1")
{
Console.Clear();
while(done != true) { Console.WriteLine("say something"); string input2 = Console.ReadLine(); Console.WriteLine(input2); Console.WriteLine(" "); Console.WriteLine("arw you done?"); string input3 = Console.ReadLine().ToLower(); if(input3 == "y") { done = true; Console.Clear(); Console.WriteLine("bye!"); } else { done = false; Console.Clear(); Console.WriteLine("choose an option"); Console.WriteLine("Start(1)"); Console.WriteLine("help(2)"); Console.WriteLine("exit(3)"); userInput = Console.ReadLine(); } } } else if (userInput == "2") { Console.Clear(); Console.WriteLine("rules here");
} else if (userInput == "3") { finished = true; Console.Clear(); Console.WriteLine("bye bye"); } else { Console.Clear(); Console.WriteLine("thats not an answer"); Console.WriteLine("try again"); Console.WriteLine(" "); Console.WriteLine("choose an option"); Console.WriteLine("Start(1)"); Console.WriteLine("help(2)"); Console.WriteLine("exit(3)"); userInput = Console.ReadLine(); } } while (finished != true); this is the new and it works
while(done != true) { Console.WriteLine("say something"); string input2 = Console.ReadLine(); Console.WriteLine(input2); Console.WriteLine(" "); Console.WriteLine("arw you done?"); string input3 = Console.ReadLine().ToLower(); if(input3 == "y") { done = true; Console.Clear(); Console.WriteLine("bye!"); } else { done = false; Console.Clear(); Console.WriteLine("choose an option"); Console.WriteLine("Start(1)"); Console.WriteLine("help(2)"); Console.WriteLine("exit(3)"); userInput = Console.ReadLine(); } } } else if (userInput == "2") { Console.Clear(); Console.WriteLine("rules here");
} else if (userInput == "3") { finished = true; Console.Clear(); Console.WriteLine("bye bye"); } else { Console.Clear(); Console.WriteLine("thats not an answer"); Console.WriteLine("try again"); Console.WriteLine(" "); Console.WriteLine("choose an option"); Console.WriteLine("Start(1)"); Console.WriteLine("help(2)"); Console.WriteLine("exit(3)"); userInput = Console.ReadLine(); } } while (finished != true); this is the new and it works
Nice, in the first scenario I understand it didn't hit the break. As you understand the break works perfectly so if it's not breaking the loop is because it wasn't hit it him.
As a sugestion if you are trying to break the loop by a condition (done variable in your case) instead of break use continue;. If you will break, just don't have any condition