No, a for-statement is a statement and not an expression. The condition expression needs to be an expression.
However you can of course do what you want in other ways, even without having to resort to goto. One way is to check the loop condition again and see if it failed (otherwise you must have broken out of the loop, given a few assumptions):
int i;
for( i=0; i<10; i++)
if( i>6 )
break;
if( i<10 ) // Loop condition still true, so we must have broken out
cout << "i went till 6";
else // Loop condition not true, so we must have finished the loop
cout << "i went till 10";
If that's not possible you could use a variable to indicate whether you broke out of the loop or not. Or you could wrap the loop in a function and use the return value to indicate whether it broke out or finished:
bool broke_out_of_loop(void) {
for( int i=0; i<10; i++)
if( i>6 )
return true;
return false;
}
void your_function(void) {
if( broke_out_of_loop() )
cout << "i went till 6";
else
cout << "i went till 10";
}
iever be10when you alwaysbreakafter6? Why not just sayfor(int i = 0; i <= 6; ++i)?ifmust be a value.fordoes not return a value, so this is not valid. There are, however, other ways to do what you want as other posters have indicated.for elseof python.