More Complexicity in IF-Else Condition with a break statement

I’m having a page like this

enter image description here

if the item Name match with the payment Name then some calculations will take part.

for that the code I have written is
i belongs to item & j belongs to payment Name

for(integer i=0;i<item.size();i++){ // say here i=0;
    for(integer j=0;j<payment.size();j++){ //j=0
         if(item[i].Name == payment[j].payment_name__c){ //condition satifies for the first time
           //code excecuted 
           // then here j shouldn't become 1 since it will go to else statement for i=0
           break;
         }else if(item[i].Name != payment[j].payment_name__c && item.size() >= payment.size()){
           //some code;
           break;
         }
    }
}

Here what I want is for i=0;j=0 should excecute (since the names of both Matches) and for i=1;j=1; should excecute (again the name matches) both satisfying only the if condition and not entering into the else if condition.

The purpose of else if condition here is when No name matches between i & j then only it should excecute. Hope this gives u the idea.

How should I put the conditions for different cases to satisfy.Ideas Please

Answer

Let’s reproduce it with very simple example. Say we have two arrays x and y with some values and want to compare each x with each y. If x is present in y we will output the log and break the loop and set the boolean variable to true. Then after the inner loop is finished we will check this boolean. If it is false then the x is not present in y (let us call it the else):

List<String> items = new List<String>{'Test1','Test2','Test3'};
List<String> payments = new List<String>{'Test1','Test2'};

Boolean isPresent;

for(String item : items){
    isPresent = false;
    for(String payment : payments){
        if(item == payment){
            System.debug('*** The value ' + item + ' from list-X is present in the list-Y.');
            isPresent = true;
            break;
        }
    }
    if(!isPresent){
        System.debug('*** The value ' + item + ' from list-X is NOT present in the list-Y.');
    }
}

The result:

enter image description here

Attribution
Source : Link , Question Author : Eagerin Sf , Answer Author : Sergej Utko

Leave a Comment