Search code examples
javabukkit

Continue parts of nested loop if condition of one loop is not met at exactly the beginning


I have exactly 3-level nested loop. If an input is exactly 0, the execution of other for loops should continue so normal nested loop won't work I tried thinking of a workaround but i have no idea how would i make it work. Any kind of solution is welcome

I have 3 inputs that are some kind of "multipliers". If the value is 0, the execution should still continue.

Example:

int coni=2, conj=-2, conk=0;
//intermediate values that tell should it be negative or positive
//calculated from the beginning values, but ill get rid of the code
int ti=1,tj=-1,tk=0;

for(int i=0;i<(int)Math.abs(coni);i++){
    for(int j=0;i<(int)Math.abs(conj);j++){
        for(int k=0;i<(int)Math.abs(conk);k++){
            //will never get executed
            function(i*ti,j*tj,k*tk);
        }
    }
}

What i want to happen:

function(0,0,0);
function(0,-1,0);
function(1,0,0);
function(1,-1,0);

Solution

  • Setting <= instead of < in your for loop will help you achieve what you want.

    for(int i=0;i<=(int)Math.abs(coni);i++){
        for(int j=0;i<=(int)Math.abs(conj);j++){
            for(int k=0;i<=(int)Math.abs(conk);k++){
                //will never get executed
                function(i*ti,j*tj,k*tk);
            }
        }
    }
    

    This will execute

    function(0,0,0)