5

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

  • 1
    I'm not quite clear on what your question is asking. Can you elaborate or rephrase your question? Why do you use break? – ca_peterson Jun 19 '13 at 11:16
  • Remove the break? – Sergej Utko Jun 19 '13 at 11:27
  • if I do so for the same i=0;j=1 also excecutes.But here what really I want is for i=0;j=0 should excecute and for i=1;j=1;should excecute both satisfying only the if condition and not else if condition – Eagerin Sf Jun 19 '13 at 11:32
4

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

  • for the same test1 of x test1 of y matches so here test2 of y shouldn't execute and for test2 of x only test2 of y should excecute ignoring the else conditions in both cases.When no Name matches then only else condition should execute – Eagerin Sf Jun 19 '13 at 12:02
  • @EagerinSf I'm trying to understand what's your point. Updated my example. – Sergej Utko Jun 19 '13 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.