- Home /
Not all code paths return a value in boolean loop
This is the loop that is giving me a problem. I am trying to make it so that if all requirements in an array of booleans are true, or if there are no requirement booleans in the array, the master boolean returns true. Otherwise, if not all requirement booleans are true the master boolean returns false. I got the code to work somewhat as a void, but if the last boolean in the array returned true it caused the master boolean to return true as well.
private bool AllRequirements {
get {
for (int i = 0; i < Requirement.Length; i++)
{
if (Requirement[i] == true)
{
return true;
}
else if (Requirement[i] == false)
{
return false;
}
}
}
}
Answer by Dragate · Jan 01, 2018 at 07:52 PM
What it's telling you is that you need to ENSURE the function will return something. The compiler sees that you only return a value when certain conditions are met. What happens if neither of those 2 if-statements are true?! What does the function return?!
Aside from that, you got a logical error. You return a value from the very 1st array element (which is ok IF you found a req not met). You're not checking the rest of the elements.
I would resolve this with the below code. Basically, code will return early if it finds a req not met. Otherwise (all reqs met), it will reach the end of the loop and return true.
private bool AllRequirements {
get {
for (int i = 0; i < Requirement.Length; i++){
if (Requirement[i] == false){
return false;
}
}
return true;
}
}
Answer by Bunny83 · Jan 01, 2018 at 08:05 PM
You have several problems here. Some logic and some semantic problems. Even if Requirements is a bool array, in which case your two conditions are mutually exclusive, it's still possible that your array is empty. So the for loop wouldn't run at all. In this case you don't return anything. This is impossible for a method with a return type \ property getter. You always must return a value.
Next thing is you do not check if all Requirements are true. Your logic only checks the first one. Just imagine there are 3 requirements. At index 0 you first check if that requirement is true and if so you immediately return true without checking the others. However if the first is false your second if would become true and you immediately return false.
What you probably want is something like this:
private bool AllRequirements {
get {
for (int i = 0; i < Requirement.Length; i++)
{
if (Requirement[i] == false)
{
return false;
}
}
return true;
}
}
As soon as you hit one requirement that is false we terminate the loop and return false. However if the loop manages to get through all Requirements we know all have to be true. It will also return true when there is no requirement at all.
Your answer
Follow this Question
Related Questions
How can I do a foreach loop for an array of booleans? 1 Answer
How can I move back to Element 0 in an array to reset a loop? 1 Answer
question about array or list of boolean 1 Answer
Check if boolean is true on my gameObjects from my array 1 Answer
How to add a Sprite Rollover Image using a Button Array 0 Answers