Question: I'm having trouble understanding a bool concept in the book. Below is some code presented in Chapter 8 pg. 458 of Starting Out with C++
I'm having trouble understanding a bool concept in the book. Below is some code presented in Chapter 8 pg. 458 of Starting Out with C++ From Control Structures Through Objects 8th Edition (the part I'm confused about is in bold):
int searchList(const int list[], int numElems, int value) { int index = 0; // Used as a subscript to search array int position = 1; // To record position of search value bool found = false; // Flag to indicate if the value was found while (index < numElems && !found) { if (list[index] == value) // If the value is found { found = true; // Set the flag position = index; // Record the value's subscript } index++; // Go to the next element } return position; // Return the position, or 1 }
My question is why is the while statement checking if "found" is true instead of false? Wouldn't that mean that the while statment would continue even when found is true?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
