-3

I don't know how to iterate my for loop. I have some values in V[i] (assume V[i] as double) already. Now V[i] is updated by below for loop. I want to subtract both old V[i] and new V[i] and check whether it is greater than 0.00005. Once this condition fails the for loop,that is iteration, must be terminated. Important point to be noted is that while checking that condition it must be done for all V (0,1,2...n) values and then the loop must be executed. I hope that my question is clear to understand.If not please tell me. I will elaborate.

for(int i=0;i<n;i++)
{
  if("some_statement")
      {
        //find V[i]
      }
  else if("some_statement")
      {
        //find V[i]
      }
}
remudada
  • 3,751
  • 2
  • 33
  • 60
gkn06
  • 95
  • 8
  • What you tried so far? – Siva Charan Mar 15 '14 at 18:58
  • Does it mean, that if for some i Vnew[i] - Vold[i] < 0.0005, the entire for loop should be terminated? – radekEm Mar 15 '14 at 18:58
  • @guitar_freak before for loop is executed v has some value.Let say that v as VOLD.After for loop v is updated.Let u say this as VNEW.If VOLD-VNEW<0.00005 the entire for loop must be terminated.If not VOLD is updated with recently found VNEW and again VNEW is updated by for loop and the same condition to check repeats... – gkn06 Mar 15 '14 at 19:01
  • @gkn06 all said, you should format your code properly before posting – Dheeraj Bhaskar Mar 15 '14 at 19:07
  • Do you know of `break` and `continue`? – fge Mar 15 '14 at 19:21
  • @gkn06 Oh, god please clarify on what you want to do. Leave old and new Vs, you're finding that too tough to explain. Give an example will ya? – Dheeraj Bhaskar Mar 15 '14 at 19:22

5 Answers5

0

you can use

if( (Your value)>0.005 ) {

break;

}

Break will terminate your for loop, try to read more about break, when ever it will get encountered in side any loop it will just break or stop the execution of that look

pushpendra chauhan
  • 2,205
  • 3
  • 20
  • 29
0

Use below code continue;, this keyword terminates loop only for that iteration and your loop will execute for all V (0,1,2...n) values.

if( (Vold[i]-Vnew[i)>0.005 ) { continue; }

To find more about continue try this link

Community
  • 1
  • 1
Mobi
  • 645
  • 3
  • 6
  • 14
0
for(int i=0; i<n; ++i){
   if(v_old[i] - v_new[i] <= 0.0005) {
         break;
   }
}

Are you looking for that?

radekEm
  • 4,617
  • 6
  • 32
  • 45
0

Use a temporary variable to store old V[i]

//a temp var to store found V[i] Values
Double old_Vi;

for(int i=0;i<n;i++)
{
  if("some_statement")
      { 
        //find V[i]
        //now compare
        if(old_Vi !=null && (V[i]-old_Vi)<0.00005)
        break;
        
        //store new V[i] as old Vi
        old_Vi=V[i];
      }
  else if("some_statement")
      {

        //find V[i]
        //now compare
        if(old_Vi !=null && (V[i]-old_Vi)<0.00005)
        break;

        //store new V[i] as old Vi
        old_Vi=V[i];
      }
}
Community
  • 1
  • 1
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
  • all the value of V must be checked and then the loop must be executed...This too doesnt works – gkn06 Mar 15 '14 at 19:18
  • checking all the values of V is done in the loop (which means you already executed it); then you are saying execute the loop. What do you mean? – Dheeraj Bhaskar Mar 15 '14 at 19:20
  • I mean that if any value of V violates the condition satisfied by V must also get update by for loop..Is this clear?? – gkn06 Mar 15 '14 at 19:25
  • V (the entire array) must get update to what?. Let's be clear V[i] are the elements; V is the array; old_Vi is an old value of V[i] or null if no value of V[i] was ever found. Now explain. – Dheeraj Bhaskar Mar 15 '14 at 19:33
  • the entire array of V must be updated by for loop if any one value of V violates the condition.old _Vi must be updated by new value of V which is founded by for loop. – gkn06 Mar 15 '14 at 19:35
  • I still didnt found the exact answer.Found my answer partially.For some problem(sums) that wont works.. – gkn06 Mar 15 '14 at 19:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49821/discussion-between-dheeb-and-gkn06) – Dheeraj Bhaskar Mar 15 '14 at 19:38
0

I think I finally understand what you want to do...

final double threshold = 0.00005;

boolean failed = false;
do {
    for (int i = 0; i < V.length; i++) {
        double Vold = V[i];
        V[i] = updateValue(V[i]);
        if (V[i] - Vold > threshold) {
            failed = true;
            // Not sure if you want this break in here - it's still unclear what you really want
            break;
        }
    }
} while (failed);

Where updateValue() is some function or expression that implements the desired update.

wakjah
  • 4,541
  • 1
  • 18
  • 23
  • What is your expected result? As written, the above code has no accessible result, but your desired result is unclear from the question. – wakjah Mar 15 '14 at 19:12
  • For example V[0] has value as 5.321.By for loop the value will be updated Say that new value of V[0] as 5.3214.I want to subtract both the new and old.Not successive values of V – gkn06 Mar 15 '14 at 19:16
  • bro is this works for all V values.My question is if one value of V fails the condition again all the values must be updated by for loop – gkn06 Mar 15 '14 at 19:22
  • So if it fails once during the `for` loop, stop the `for` loop and try again, and keep going until none of the updated values fail the test? – wakjah Mar 15 '14 at 19:23