Jump to content

break;

alphabeta
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 !? 

Link to comment
Share on other sites

Link to post
Share on other sites

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
Share on other sites

Link to post
Share on other sites

loop

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

 

continue will do what exactly !? 

Link to comment
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
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

×