❔ Breaking an infinite while loop

I need help with stoping an infinite loop when the search word is found
36 Replies
Cattywampus
Cattywampus2y ago
put a boolean or something
while(true){if(found) break;}
while(true){if(found) break;}
TheRanger
TheRanger2y ago
or use LINQ Methods if you can
The king of kings
Ok! This is my project
int first = 0;
int last = digitList.Count - 1;
while (first <= last)
{
int between = (first + last) / 2;
if (key > digitList[between])
{
first = middle + 1;
}
else if (key < digitList[between])
{
last = between - 1;
}
otherwise
{
Console.WriteLine($"\n\tThe number they searched for is on element {between} in the number list.");
}
if (first > last)
{
Console.WriteLine("\n\tThe search failed");
DigitPrint(digitList); // Print the entire list using our method.
}
otherwise
{
Console.WriteLine("\n\tThe numbers in your list need to be sorted. Run a sort before searching."); // The user needs to do a sort.
}

}
int first = 0;
int last = digitList.Count - 1;
while (first <= last)
{
int between = (first + last) / 2;
if (key > digitList[between])
{
first = middle + 1;
}
else if (key < digitList[between])
{
last = between - 1;
}
otherwise
{
Console.WriteLine($"\n\tThe number they searched for is on element {between} in the number list.");
}
if (first > last)
{
Console.WriteLine("\n\tThe search failed");
DigitPrint(digitList); // Print the entire list using our method.
}
otherwise
{
Console.WriteLine("\n\tThe numbers in your list need to be sorted. Run a sort before searching."); // The user needs to do a sort.
}

}
It's an assignment consisted of Binary search I wanna learn the bug
Kouhai
Kouhai2y ago
Couple of notes, if digitList doesn't have any items, no message will be printed out informing the user of whether something was found or not Also assuming by otherwise you mean else This code path will get executed even though it shouldn't
otherwise
{
Console.WriteLine("\n\tThe numbers in your list need to be sorted. Run a sort before searching."); // The user needs to do a sort.
}
otherwise
{
Console.WriteLine("\n\tThe numbers in your list need to be sorted. Run a sort before searching."); // The user needs to do a sort.
}
You also keep executing even after finding the number You can just break;
Cattywampus
Cattywampus2y ago
you're doing your loop like this while (first <= last) this part --> if (first > last) in your loop is useless also, proly fix the formatting of your snippet above, kinda infuriating to stare at it when on the ide you're using, they usually would tell you what's wrong with your loop and whatnot
The king of kings
I forgot the paste the whole block
case 3: // Here the user should be able to search among the numbers with a binary search. (BINARY SEARCH OF NUMBERS)
Console.Clear();
if (numberList.Count > 0)
{
if (intSort)
{
Console.WriteLine("\n\tWhich number do you want to search?");
Int32.TryParse(Console.ReadLine(), out int key); // Receives the user's search
if (key < 0) key = 0; // Makes sure the search is on a positive number
case 3: // Here the user should be able to search among the numbers with a binary search. (BINARY SEARCH OF NUMBERS)
Console.Clear();
if (numberList.Count > 0)
{
if (intSort)
{
Console.WriteLine("\n\tWhich number do you want to search?");
Int32.TryParse(Console.ReadLine(), out int key); // Receives the user's search
if (key < 0) key = 0; // Makes sure the search is on a positive number
You're right. That message should be prompted too. Well! There's this message that you mentioned, but I didn't show it from the beginning. This is the whole block btw.
case 3: // Here the user should be able to search among the numbers with a binary search. (BINARY SEARCH OF NUMBERS)
Console.Clear();
if (numberList.Count > 0)
{
if (intSort)
{
Console.WriteLine("\n\tWhich number do you want to search?");
Int32.TryParse(Console.ReadLine(), out int key); // Receives the user's search
if (key < 0) key = 0; // Makes sure the search is on a positive number
nt first = 0;
int last = digitList.Count - 1;
while (first <= last)
{
int between = (first + last) / 2;
if (key > digitList[between])
{
first = middle + 1;
}
else if (key < digitList[between])
{
last = between - 1;
}
otherwise
{
Console.WriteLine($"\n\tThe number they searched for is on element {between} in the number list.");
}
case 3: // Here the user should be able to search among the numbers with a binary search. (BINARY SEARCH OF NUMBERS)
Console.Clear();
if (numberList.Count > 0)
{
if (intSort)
{
Console.WriteLine("\n\tWhich number do you want to search?");
Int32.TryParse(Console.ReadLine(), out int key); // Receives the user's search
if (key < 0) key = 0; // Makes sure the search is on a positive number
nt first = 0;
int last = digitList.Count - 1;
while (first <= last)
{
int between = (first + last) / 2;
if (key > digitList[between])
{
first = middle + 1;
}
else if (key < digitList[between])
{
last = between - 1;
}
otherwise
{
Console.WriteLine($"\n\tThe number they searched for is on element {between} in the number list.");
}
if (first > last)
{
Console.WriteLine("\n\tSearch failed");
DigitPrint(digitList); // Print the entire list using our method.
}
otherwise
{
Console.WriteLine("\n\tThe numbers in your list need to be sorted. Run a sort before searching."); // The user needs to do a sort.
}
}
}
otherwise
{
Console.WriteLine("\n\tThere are missing entries in the number list. Generate some numbers first."); // The user has not generated a value for the list.
}
}
MenuExit(); // Calls our standard menu exit.
breaks;
if (first > last)
{
Console.WriteLine("\n\tSearch failed");
DigitPrint(digitList); // Print the entire list using our method.
}
otherwise
{
Console.WriteLine("\n\tThe numbers in your list need to be sorted. Run a sort before searching."); // The user needs to do a sort.
}
}
}
otherwise
{
Console.WriteLine("\n\tThere are missing entries in the number list. Generate some numbers first."); // The user has not generated a value for the list.
}
}
MenuExit(); // Calls our standard menu exit.
breaks;
break what? I tried it in multiple places but didn't see the right output.
Pobiega
Pobiega2y ago
break; stops the loop. thats all it does. so you just need to figure out when to call it.
The king of kings
You're right. I need to make a condition that ends the loop when the searched element is found not just stoping the loop because we would like to, makes sense?
The king of kings
I mean when I use the break inside the else statement, it works well, it breaks the loop when found, but in the second screenshot when I input a wrong number, it needs to show a message that the element wasn't found instead of those copied numbers.
The king of kings
@Pobiegacould you please review the small change that I made?
Kouhai
Kouhai2y ago
@Faraj You need to keep track of whether you found the item or not
The king of kings
Ok! Yes, you're right. But does the break position is correct or not?
Kouhai
Kouhai2y ago
It's correct, but in the case you're tracking whether the item was found or not, you can just remove break; and instead have while(first <= last && !found) And set found to true instead of break
The king of kings
Ok! Just like they did it here.
Kouhai
Kouhai2y ago
Here it's different Because they return the index
The king of kings
You're awesome girl, you always show up to offer some help, I really appreciated. Yeah! It's alphabetically algo.
Kouhai
Kouhai2y ago
I'm not entirely sure what do you mean by alphabetically algo, but my point was that they control code flow with the return statement, in your case you just print out a message to the user So while you could do return; I think having found is more clear imo
The king of kings
sorry I meant in the first code in this link, they're doing almost the same https://stackoverflow.com/questions/26113161/binary-search-of-strings-in-an-array-in-c-sharp
Stack Overflow
Binary search of strings in an array in C#
Ok so I have been at this same error for about 18 hours and am totally lost. What I am trying to do is conduct a binary search where the search begins in the middle of the array and then eliminates...
Kouhai
Kouhai2y ago
The code in the link could just use a return statement instead
The king of kings
Ok! I meant a Binary search algorithm searching for string type elements through alphabetically. You're right. Since I don't have any method defined, therefore I can't return a value if found or not found, so I would go with your approach instead. Ok! Based on your knowledge. so where should I set the found to true?
Kouhai
Kouhai2y ago
In the code path that checks if the item is matching Though tbh, I would just refactor that method and make it return an index instead When you think about a Search method you think about something that will return and index or something, you don't expect it to print the found index. So instead you make the method return an index And make the caller check whether the index was valid or not If the returned index is -1, it's obviously invalid and nothing was found
The king of kings
Ok! Your assumption is very correct and I noticed all the examples on Google they're using methods and returning either found or -1 I totally agree. But in this assignment I was given pseudocode and they want me to implement it in this approach.
The king of kings
as you can see they're mentioning any method or returning
The king of kings
so what do you think? should I ignore their approach and go with your approach by creating a method
Kouhai
Kouhai2y ago
I'm not entirely sure how strict the tutor would be 😅 So I guess keep your implementation
The king of kings
Ok! Yeah! I guess this is what I can do for now at least. so where did you think the found variable should be located? my friend
The king of kings
I mean in this approach
Keyinator
Keyinator2y ago
must be created outside the while loop so the condition can be checked. Then updated inside
The king of kings
Ok! Just like when we do incrementing, we put it underneath the second brackets of the while loop. Thanks a lot
The king of kings
Awesome no error
The king of kings
Let me test the code It's not working actually
Kouhai
Kouhai2y ago
Yes, it'll not work, take a look at where you set found ThumbsUpSmile
Servator
Servator2y ago
CancellationToken ftw!
The king of kings
Ok
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?