m2rt
m2rt

Reputation: 157

Executing for loop inside if statement

I can't figure it out why my program won't start executing the for loop: the logic for the for loop is correct, but when the program runs it just skips the for loop, without executing it. If i is equal to count, it should read last remaining elements from array called se and write to array finallyDone.

while(check && i < len){
    int fi = first[i];
    int se = second[j];
    if(fi < se){
        finallyDone[count] = fi;
        i++;
    }
    else{
        finallyDone[count] = se;
        j++;
    }
    int l;
    if(i >= len){
        for(l = count; l < len * 2 - count; l++){
            finallyDone[count + 1] = se;
        }
        check = false;
    }
    count++;
}

Upvotes: 0

Views: 1808

Answers (1)

Ward
Ward

Reputation: 64

This is where it goes wrong:

while(check && i < len){
   //and later
    if(i >= len){

You can only walk to the IF-statement if i is smaller than len, but you will only walk into the IF-statement if i is equal to len or bigger. Those two statements are contradictory.

Upvotes: 3

Related Questions