Jump to content

break;

Go to solution Solved by madknight3,

@alphabeta - Note that replacing break with continue in your original example doesn't do anything useful.

for(int r=0;r<cases;r++){    if(r%2==0 && x[r]%2==0 || r%2==1 && x[r]%2==1 )    {        a=false;        continue; // this doesn't do anything because if it's getting in the if block, it wont be getting in the else block anyway and you don't execute any other code in the loop    }    else    {        a=true;    }    // If you have extra code here, then continue would ignore it. So you only need it if extra code goes after the else statement that you want to ignore.}

Just wanted to make sure it's clear.

 

 

Also, just wanted to mention this.

// these kinds of statementsif (condition) {    variable = true;}else {    variable = false;}// can be rewritten asvariable = condition// in your case it's the oppositeif (condition) {    variable = false;}else {    variable = true;}// so you just negate itvariable = !condition// Example using your codefor (int r=0;r<cases;r++){    a = !(r%2==0 && x[r]%2==0 || r%2==1 && x[r]%2==1);}// It works the same so it's best to go with whatever you think is easier to understand.
for(int r=0;r<cases;r++){    if(r%2==0 && x[r]%2==0 || r%2==1 && x[r]%2==1 )    {        a=false;        break;    }    else    {        a=true;    }}

in a code like this does break; get out of if condition or the loop !? 

 

loop

if you want to skip that else and continue with for than use continue;

Link to comment
https://linustechtips.com/topic/272103-break/#findComment-3691476
Share on other sites

Link to post
Share on other sites

continue will do what exactly !? 

will skip everything in front of it and increment r and do what for is meant to do

 

http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm

for(i = 0; i < 10; i++){       if(i==5)       {                 continue;       }       printf("\n%i", i);}

it will print:

1

2

3

4

6

7

8

9

Link to comment
https://linustechtips.com/topic/272103-break/#findComment-3691623
Share on other sites

Link to post
Share on other sites

@alphabeta - Note that replacing break with continue in your original example doesn't do anything useful.

for(int r=0;r<cases;r++){    if(r%2==0 && x[r]%2==0 || r%2==1 && x[r]%2==1 )    {        a=false;        continue; // this doesn't do anything because if it's getting in the if block, it wont be getting in the else block anyway and you don't execute any other code in the loop    }    else    {        a=true;    }    // If you have extra code here, then continue would ignore it. So you only need it if extra code goes after the else statement that you want to ignore.}

Just wanted to make sure it's clear.

 

 

Also, just wanted to mention this.

// these kinds of statementsif (condition) {    variable = true;}else {    variable = false;}// can be rewritten asvariable = condition// in your case it's the oppositeif (condition) {    variable = false;}else {    variable = true;}// so you just negate itvariable = !condition// Example using your codefor (int r=0;r<cases;r++){    a = !(r%2==0 && x[r]%2==0 || r%2==1 && x[r]%2==1);}// It works the same so it's best to go with whatever you think is easier to understand.
Link to comment
https://linustechtips.com/topic/272103-break/#findComment-3694628
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×