Loop for( statement expression; action ) body
loop is just a while loop
{
statement;
while ( expression )
{
body
label_for_continue:
action;
}
}
So you can see that everything declared inside will have its life ending outside of for
. You can use break
to stop loop prematurely and continue
to jump back to beginning of loop after performing action
( equals to goto label_for_continue
), this makes it a little more versatile than while
where continue
jumps directly to “beginning”.
Any or all parts may be omitted or used in ways allowed by syntax of language, resulting in use of external counter, a “forever” loop for(;;)
or pre-increment loop for(int i = MAX; i-->0;)
. A special case of for loop use is range-based loop.
What you need to do is to use some external variable to mark your current state. Let it be false
by default, because if your loop exits normally, it means that you didn’t found a match. If you found match, you would “short-circuit” loop by break
after setting flag to true:
bool is_vowel = false;
for (int i = 0 ; i<10 ; i++) {
if (x == vowel[i]) {
is_vowel = true;
break;
}
}
solved using loop for only if statement and then print else statement [closed]