Error cs0139 no enclosing loop out of which to break or continue

Here is my C# coding public string RemoveFirstSpaces (string str) { if(str.Length > 0) { while(str[0] == " ") { str = str.Substring(1, str.Length - 1); ...

Here is my C# coding

public string RemoveFirstSpaces (string str)
{
    if(str.Length > 0)
    {
        while(str[0] == " ")
        {
            str = str.Substring(1, str.Length - 1);
            if(str.Length <= 0)
            {
                break;
            }
        }
    }
    return str;
}

How could i break when it comes inside the if stmt..

asked May 17, 2013 at 8:54

SaravanaKumar's user avatar

4

In your current code, the only mistake is the check in the while it should be:

 while (str[0] == ' ')

since str[0] is a character and currently you are comparing it with " " which is string.

Although a simpler way to remove starting space would be to use String.TrimeStart

public string RemoveFirstSpaces (string str)
{
  return str.TrimStart();
}

answered May 17, 2013 at 9:05

Habib's user avatar

HabibHabib

217k27 gold badges402 silver badges429 bronze badges

0

breakc++

Here is my C# coding

public string RemoveFirstSpaces (string str)
{
    if(str.Length > 0)
    {
        while(str[0] == " ")
        {
            str = str.Substring(1, str.Length - 1);
            if(str.Length <= 0)
            {
                break;
            }
        }
    }
    return str;
}

How could i break when it comes inside the if stmt..

Best Solution

In your current code, the only mistake is the check in the while it should be:

 while (str[0] == ' ')

since str[0] is a character and currently you are comparing it with " " which is string.

Although a simpler way to remove starting space would be to use String.TrimeStart

public string RemoveFirstSpaces (string str)
{
  return str.TrimStart();
}

Related Solutions

C# loop – break vs. continue

break will exit the loop completely, continue will just skip the current iteration.

For example:

for (int i = 0; i < 10; i++) {
    if (i == 0) {
        break;
    }

    DoSomeThingWith(i);
}

The break will cause the loop to exit on the first iteration — DoSomeThingWith will never be executed. This here:

for (int i = 0; i < 10; i++) {
    if(i == 0) {
        continue;
    }

    DoSomeThingWith(i);
}

Will not execute DoSomeThingWith for i = 0, but the loop will continue and DoSomeThingWith will be executed for i = 1 to i = 9.

Python – How to break out of multiple loops

My first instinct would be to refactor the nested loop into a function and use return to break out.

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

John Lukacs

How do I break out of a loop;
My code wont pass in the second question on the last challenge
I think my code is looping without an end. Im down to one error I dont know what to do


Program.cs

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print "Yay!": ");


            try{
                string numofTimes= Console.ReadLine();
            int num =int.Parse(numofTimes);
                 for(var i=0; i<num; i++){
         Console.WriteLine(""Yay!"");
                     break;
            }}
                catch(FormatException){
                Console.WriteLine("You must enter a whole number.");
                    continue;



            }
        }
    }
}

3 Answers

james south

seal-mask

you don’t need the continue or the break. the continue is not in a loop so it is not doing anything. the break is causing the program to end prematurely, before all the yays have been printed. just remove the break and the continue and the code passes tasks 1 and 2.

John Lukacs

Hi James I have tried that It still does not work

Kyle Jensen

PLUS

You don’t have a need for a break statement. Your program will end when ‘i’ hits the ceiling input provided from the user — the second arg in your loop. The reason the compiler complains about ‘continue’ is because ‘continue’ can only be implemented within the loop. The program is going to continue regardless. The point of the continue, within a loop, is to avoid execution upon a certain condition. Let’s take a look at an example that assumes we’re using System; and that the user entered 10 times.

for(var i = 0; i < 10; i++) { 
    //just for fun lets print the 7th iteration as its own thing
    if(i == 6) { 
        Console.WriteLine("I'm " + (i + 1) + " and I'm hungry - I'm going to eat number " + (i + 3) ); 
    } 
    //now lets see the continue statement in action by skipping printing the 9
    else if(i == 8) {
        continue; 
}
    else { 
        Console.WriteLine("iteration: " +  (i + 1)); 
    } 
}

if you were to take out the if( i == 6) line each iteration would print exactly the same except for the number 9. 9 is skipped because of the continue within our conditional.
I hope this helps!!

Понравилась статья? Поделить с друзьями:
  • Error cs0120 an object reference is required for the non static field method or property
  • Error cs0119 gameobject is a type which is not valid in the given context
  • Error cs0119 expression denotes a type where a variable value or method group was expected
  • Error cs0118 editor is a namespace but is used like a type
  • Error cs0117 unity