using break !!

Hi guys I want ur help in this plz, I want when the first if gives true, do the statements and break(do not go the else if).

and if the 1st if gives false go and check else if
Please how can I do that ??

for(int i=0; i<counter; i++){//counter value comes from outside this function

if(condition1 && condition2 && condition3){
......
.......
}

else if(condition1 && condition2 && opposite(condition3)){
.....
.... }
}// end for

Last edited on
I tried using break as below!

for(int i=0; i<counter; i++){//counter value comes from outside this function

if(condition1 && condition2 && condition3){
......
.......
break; does not work ??
}

else if(condition1 && condition2 && opposite(condition3)){
.....
.... }
}// end for
Don't get into the habit of using break for your applications, it's kind of a hackey solution. Putting loops like this inside of their own functions from which they can return instead of "jumping" out of makes it much easier to follow the flow of data.

Also, you need to work on your program flow. This "if...else" should be encapsulated in another if statement like this:
1
2
3
4
5
6
7
8
9
10
11
if(condition1 && condition2)
{
    if(condition3)
    {
         //code code code
     }
     else
     {
          //other code other code
      }
}


And listen to shacktar the "else" condition is NOT EXECUTED when the "if" statement is true. If you are seeing evidence to the contrary I would guess there is something wrong with your "for loop" instead.
Thanks for the reply
but I need to do the same code in both if statement guys, and I need the priority for 1st if and then 2nd if

for(int i=0; i<counter; i++){//counter value comes from outside this function

if(condition1 && condition2){
action 1
action 2
}

else if((condition1) && opposite(condition2)){
action 1
action 2
}
}// end for
Last edited on
You're over-complicating this then, do you mind posting a little more code and giving us more detail on your objective?

for(int i=0; i<counter; i++){//counter value comes from outside this function
x=myarray[i];

if(x==b)&& (x==c)){

f=x;
}

else if (x==b)&& (x!=c)){
f=x;
}
}// end for
Last edited on
This should work. Assuming that 'a' 'b' and 'c' are all equal. Are you sure that the problem you are seeing isn't that both conditions do exaclty the same thing?
to be clear more (x!=b && x==c) just forget x==a
[code] "Please use code tags" [/code]
¿what do you want to do? (no how you are doing it)
Thanks ne555 for the reply
but I need to do the same code in both if statement guys, and I need the priority for 1st if and then 2nd if

for(int i=0; i<counter; i++){//counter value comes from outside this function

if(condition1 && condition2){
action 1
action 2
}

else if((condition1) && opposite(condition2)){
action 1
action 2
}
}// end for


for(int i=0; i<counter; i++){//counter value comes from outside this function
x=myarray[i];

if(x==b)&& (x==c)){

f=x;
break;//does not work
}

else if (x==b)&& (x!=c)){
f=x;
}
}// end for
Last edited on
Topic archived. No new replies allowed.