I'd like to get out of a loop without breaking and still continuing with my code. For instance, say I have a for loop that goes over a length of a word, and then I have an if loop that checks a condition, does something if true, and if its false, I'd like it to exit that if else tree, how would I do that?
I wish I could make it more clear, but here's the code. Please don't steal my half-assed written code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for(int i = 0;i<my_word.length();i++)
{
if(guess == my_word.at(i))
{
guessHold.at( i ) = guess;// If it is at the iTH spot of the word
}
else
{
if(guessHold.at( i ) == '-') // checks here if at the iTH spot there's a character not - so it doesn't overwrite.
{
// here I want it to exit this if tree and continue with the rest of the for loop.
}
guessHold.at( i ) = '-'; // If its not at the iTH spot of the word
}
}
continue (by having no rest of the for loop), goto, a single-iteration hack loop...
But I'd rethink my structure first (properly using else seems like an easy fix here), it can likely be done fairly easily without needing any of that.
I'm a little unclear as to exactly what you want, but if the only result of the else condition is to leave the if-else statement, then you probably don't need an else.
Example:
1 2 3 4 5 6 7 8 9 10 11
// Loop
for (int i=0; i < 10; i++)
{
if (i == some_value)
{
// Do something
}
// Do something 2
}
Essentially, if i isn't that value, you come out of the if statement because you never actually go into it. Like I said, you may need to clarify this a little. For example, where exactly do you want that second if statement in your code to be? Do you want it to execute regardless of whether the conditions of the first statement are met?
For loop that iterates over the length of my_word. The for loop checks the guess char that I have above, not included. The very first, and outer, if else tree, checks the first character of the my_word string to see if they're equal. If they are I want it to store that character in the guessHold string. Then in the else part of the outer tree is if its not equal to the character in the my_word string, I want it to put '-' in its spot. The whole point of it though is that I have it loop over the for loop many times. The reason I want it to break out of the else part of the outer tree is so that it doesn't put a '-' where I've already put a character.
This is supposed to be for a hangman game. You get a guess from the user. Iterate over the length of the my_word string checking to see if the user guess filled any letters. I havent gotten around to it yet but at the end, if the user hasn't lost yet, I'll have a way to check the string for any '-''s. If there arent any, that means the user will win and it'll break out.
Hopefully this sheds some light on what I want to do.
What I want out of this if tree is if theres a '-' then I want it to do nothing. I guess I could just have it do nothing literally.
1 2 3 4
if(guessHold.at( i ) == '-') // checks here if at the iTH spot there's a character not - so it doesn't overwrite.
{
// here I want it to exit this if tree and continue with the rest of the for loop.
}