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# C# Objects Loops and Final Touches Using Static Fields

what 'break' does in ForEach loop

This is a foreach loop that attacks an array of invaders one by one if invader is in range and alive. it can either shot or miss the enemy based on 'IsSuccessfulShot' is true or false.

my Question is what is the difference of the two codes below. one has a 'break' at the end of foreach, the other has nothing.

if it has break, it shoots one invader and then that's it, no matter next invader is within range or not. it just doesn't care the next invaders because it escape the foreach loop after attacking one invader.

if it has no break, it would attack one invader and then move on to the next invader and then move on to another next, which attacks multiple invaders as long as invaders are within range. which is too overpower.

is my understanding right?

public void FireOnInvaders(Invader[] invaders)
        {
         foreach(Invader invader in invaders)
            {
              if(invader.IsActive && _location.InRangeOf(invader.Location, _range))
                 { 
                  if(IsSuccessfulShot())
                  {
                    invader.DecreaseHealth(_power);
                    Console.WriteLine("shot and hit the enemy");

                    if(invader.IsNeutralized)
                    {
                      Console.WriteLine("Neutralized the enemy!");
                     }
                  }
                   else
                  {
                    Console.WriteLine("shot and missed the target");

                   }
                   break;
                 }
        }
public void FireOnInvaders(Invader[] invaders)
        {
         foreach(Invader invader in invaders)
            {
              if(invader.IsActive && _location.InRangeOf(invader.Location, _range))
                 { 
                  if(IsSuccessfulShot())
                  {
                    invader.DecreaseHealth(_power);
                    Console.WriteLine("shot and hit the enemy");

                    if(invader.IsNeutralized)
                    {
                      Console.WriteLine("Neutralized the enemy!");
                     }

                  }
                   else
                  {
                    Console.WriteLine("shot and missed the target");

                   }

                 }
        }

1 Answer

Steven Parker
Steven Parker
231,236 Points

Your description sounds right. A loop would normally repeat, but a "break" causes it to stop immediately.