Reputation: 523
The idea is that he inputs a text file, and a word number. The software will write in a new file that text but with number of words (that he inputed) per line, and few other details.
The idea is this, I made him a blacklist. The blacklist is loaded into a richbox from a file, and saved while closing application.
The thing is that I have everything set up (a function that checks if the word is in blackbox).
The software looks like this:
foreach (string word in words)
{
int blacklist = 0;
if (FindMyText(word))
{
blacklist = 1;
MessageBox.Show("Current word: " + word + " is blacklisted!");
}
else
MessageBox.Show("Word: " + word);
// the code here ... for writing in file and all that
}
The function FindMyText(word) tells me if the word is in blacklist or not.
If that function returns true, I want to step to the next word, but really don't know how to do this.
If you have some ideas, would really help me out.
Thanks you guys.
Upvotes: 3
Views: 1764
Reputation: 1066
I'm not 100% sure I understand but I think what you want is the "continue" keyword.
Once an iteration of the loop is complete it will start again until it runs out of iterations.
So in your IF/ Else statement, you want to force the loop to go onto the next word, you type continue;. This will ignore all of the preceding code in the loop and jump to the next iteration.
Does that make sense?
Upvotes: 0
Reputation: 70786
You already have the logic, just add continue
:
The continue statement passes control to the next iteration of the enclosing iteration statement in which it appears. It takes the following form:
if (FindMyText(word))
{
blacklist = 1;
MessageBox.Show("Current word: " + word + " is blacklisted!");
continue;
}
else
{
MessageBox.Show("Word: " + word);
AddWordToFile(word); // not black listed;
}
http://msdn.microsoft.com/en-us/library/923ahwt1(v=vs.71).aspx
Upvotes: 1
Reputation: 31192
You could just add the "continue" keywork to skip to the next element in the foreach iteration.
foreach (string word in words)
{
int blacklist = 0;
if (FindMyText(word))
{
blacklist = 1;
MessageBox.Show("Current word: " + word + " is blacklisted!");
// skip to the next element
continue;
}
MessageBox.Show("Word: " + word);
// the code here ... for writing in file and all that
}
or you could just split the foreach body :
foreach (string word in words)
{
int blacklist = 0;
if (FindMyText(word))
{
blacklist = 1;
MessageBox.Show("Current word: " + word + " is blacklisted!");
}
else
{
MessageBox.Show("Word: " + word);
// the code here ... for writing in file and all that
}
}
It all depends on how long the "else" part is. If it is really long, it is more readable to use continue, to put the emphasis on the skipping part.
Upvotes: 1
Reputation: 21752
in a foreach loop or any other loop you can use the continue
to skip to the next iteration so in your case you could do
foreach (string word in words)
{
var blacklist = 0;
if (FindMyText(word))
{
blacklist = 1;
MessageBox.Show("Current word: " + word + " is blacklisted!");
continue;
} else {
//...
}
}
Upvotes: 1