1

I have a While loop that reads a line of a file.txt. I also have a method named VerifyPhoto that returns true/false I want to go to next item of the while loop if the returned value is false. How Could I do that ? I tried break and return but it just leave all and back to the form...

while (!reader.EndOfStream)
  {
     if(VerifyPhoto(filed.matriculation) == false)
        {
          //go to the next line of the file.txt
        }
  }
Ghaleon
  • 1,186
  • 5
  • 28
  • 55
  • 1
    `break` to leave the current loop, `return` to leave the method entirely, `continue` to skip the current iteration and go to the next. I guess you're looking for `continue` – Nolonar Feb 04 '13 at 12:55
  • 1
    Just thought I'd mention it now, since `break`, `return` and `continue` are all related to this: `goto`. All of these keywords are so-called **jumps**. However, `goto` is old and considered bad practice, because code using `goto` are very difficult to follow. The 3 former keywords are basically all `goto`, but they are at least a lot easier to follow. Btw. I still remember the time I had to work with a language that did not implement any of those, so I had to juggle with `bool`s. It was horror to implement, and horror to debug... – Nolonar Feb 04 '13 at 13:02
  • I remeber `goto` when I was working with `C` haha! My professor called it `Spaghetti programming` '-' . Really thanks, I didn't know the difference between the `jumps` – Ghaleon Feb 04 '13 at 13:05

6 Answers6

10

You may want to get familiar with other control statement: continue

[EDIT] Newest version of the doc: continue, thanks Jeppe.

Patryk Ćwiek
  • 14,078
  • 3
  • 55
  • 76
1

continue; (some more to make it 30 chars)

paul
  • 21,653
  • 1
  • 53
  • 54
0

Depending on your actual code, maybe you could simply invert the boolean test, so you do something only if VerifyPhoto returns true:

while (...)
{
    if(VerifyPhoto(filed.matriculation))
    {
        // Do the job
    }
}
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • But one thing i'll verify is if there's an item on a `CheckedListBox` having it or not i'll do the job, but I have these other test ;s Thanks ! – Ghaleon Feb 04 '13 at 12:55
0

The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears

while (!reader.EndOfStream)
{
    if(VerifyPhoto(filed.matriculation) == false)
    {
        continue;
        //go to the next line of the file.txt
    }
}
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

Am I missing something in the way you are doing this? Do you read the first line before starting the loop? if so don't you need something like

**string line;**
while (!reader.EndOfStream)
  {
     if(VerifyPhoto(filed.matriculation) == false)
        {
          //go to the next line of the file.txt
          **line = file.ReadLine();**
        }
  }
KerryWales
  • 21
  • 1
0

If you're trying to read line by line then File.ReadLines may be useful.

Also what you're looking for is the continue statement.

string myFile = @"c:\path\to\my\file.txt";

foreach(string line in File.ReadLines(myFile))
{
    //Do stuff
    //if(!VerifyPhoto())
    //    continue;
    //Do other logic
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92