0
#include<iostream>
using namespace std;
int main()
{
  if(for(int i=0;i<10;i++)if(i>6)break;)
  cout<<"i went till 10";//execute if the if statement is true
  else cout<<"i went till 6";
}

if it hits break it should go to else. Is this even possible?Sorry for the mistakes in writing the question if any.Second attempt at asking question.

can i use any other function or statement to execute such tasks.

4
  • How will i ever be 10 when you always break after 6? Why not just say for(int i = 0; i <= 6; ++i)? Commented Apr 25, 2017 at 5:20
  • this is not the actual code i am trying to implement , it is just a simple example to illustrate what issue i want to discuss Commented Apr 25, 2017 at 5:24
  • The condition in an if must be a value. for does not return a value, so this is not valid. There are, however, other ways to do what you want as other posters have indicated. Commented Apr 25, 2017 at 5:26
  • c++ doesn't have the for else of python. Commented Apr 25, 2017 at 7:42

3 Answers 3

3

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";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Not really, as i was declared in for statement, it's not available outside of it.
@el.pescado You're correct, I moved the declaration of i outside for that purpose...
@skyking thanx,will try to impliment using bool function
0
if([&]{for(int i=0;i<10;i++)if(i>6)return false; return true;}())
   std::cout<<"i went till 10";//execute if the if statement is true
   else std::cout<<"i went till 6";
}

I would not advise this construct, but the above compiles and does what you want.

Comments

-1

No, u can't use for loop as an if else condition statement.

1 Comment

Welcome to StackOverflow. I notice you have the badge for taking the tour. Maybe you should reread the parts on how to answer. In case you wonder why you were downvoted (not by me, though I am tempted): I guess the reason of the downvoter is the non-helpful nature of your answer. The fact that it is essentially correct does not change that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.