Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# Polymorphism Virtual Methods

Karlijn Willems
Karlijn Willems
9,961 Points

Unreachable code in a for loop in C#... what am I doing wrong?

The exercise is to make an override method that returns true when the preceding number in the array is the same. I got stuck on the for loop, it says that there is unreachable code, I am can't see it. Appreciate your help. :)

SequenceDetector.cs
namespace Treehouse.CodeChallenges
{
    class SequenceDetector 
    {
        public virtual bool Scan(int[] sequence)
        {
            return true;
        }

    }

    class RepeatDetector : SequenceDetector
    {
        public override bool Scan(int[] sequence)
        {   
            for (int i = 0; i < sequence.Length; i++) 
            {
                if(i == 0)
                return false;

                if(sequence[i] == sequence[i-1])
                    {
                        return true;
                    }

                else
                    return false;
            }
        }
    }
}
RepeatDetector.cs

5 Answers

Yes, you're right, sorry, I didn't spot it.

The issue is that the loop starts with i=0 and the first test is: If i==0 return false.

This means that the loop starts and then stops immediately as i==0.

What is the test to check if i==0 for?

You haven't put {} around the first return statement after your if, meaning all the rest of the code won't ever be executed, it will hit the return and return!

Steven Parker
Steven Parker
231,268 Points

Any code following a "return" statement is "unreachable code". But the first error was "not all code paths return a value". That's pointing out that if the "for" loop completes (or if the condition is never met), there isn't any "return" statement following it to provide the necessary value.

Also, the loop should only return (with "true") if a match is found. Otherwise, it should continue checking other elements.

Karlijn Willems
Karlijn Willems
9,961 Points

that doesn't work either.... I tried it like that before.

Also, I thought an if statement should be able to do without {} when it is only one line? since the next line is another ifstatement, I don't see why that wouldn't be possible in this context?

Karlijn Willems
Karlijn Willems
9,961 Points

Thank you guys! I implemented the first ifstatement because, I thought, otherwise it would go wrong with the second statement in case i = 0 then i - 1 would be a problem... but I was thinking about it all in the wrong way... I just changed i = 0 to i =1 in the for loop and deleted the first ifstatementis the same as the preceding. But it is true if ANY value is the same... so I made an extra boolean to check if there is any value the same as the preceding one so I could make a seperate if-else statement to return true or false at the end of the method. Solved! =)