Home > Software engineering >  In a Java nested for-loop, the code works properly if separated into 2 different for-loops. If neste
In a Java nested for-loop, the code works properly if separated into 2 different for-loops. If neste

Time:06-30

I'm having some trouble with a nested for loop. After days of agonizing searching and trial and error, I finally got the math working for a temperature conversion table. When I separate this code, it works great. Celsius converts to Fahrenheit and vise versa and both sides loop properly. However, when I try and nest the for loops, one side stops working. I've tried moving my statements around, but I keep coming up with the same result which is one side will not increment while the other side does. I'm new to Java so I don't see what I'm doing wrong as I used a for loop on a previous program with success. I'm not sure what to do with the: f1 = CF(c1); I've tried it in both loops while nested. ANY insight is greatly appreciated.

static double c1, f1; // c1 is the celsius temp, f1 is the equivalent 
                      // fahrenheit temp
static double f2, c2; // f2 is the fahrenheit temp, c2 is the equivalent 
                      //   celsius temp

double start = -50;
double finish = 50;
int five = 5;

for (c1 = start; c1 <= finish; c1  = five) {                

    f1 = CF(c1);

    for (f2 = start; f2 <= finish; f2  = five){
                
        c2 = FC(f2);
        System.out.print("\n");
        System.out.format("%6.3fC %6.3fF <> %6.3fF %6.3fC", f2, c2, c1, f1);
    }
}

Subroutine CF converts Celsius to Fahrenheit, FC converts Fahrenheit to Celsius. These subroutines contain no other code other than returning the proper mathematical equation.

As stated above, when the for loops are separated, everything works properly. When the loops are nested, the outer loop stops working, while the inner loop functions properly. To clarify, the inner loop converts fahrenheit to celsius and prints them starting from -50 to 50 in increments of 5. However, this is a snippet of what I'm getting

30.000C     -1.111F <>   50.000F    122.000C
35.000C      1.667F <>   50.000F    122.000C
40.000C      4.444F <>   50.000F    122.000C
45.000C      7.222F <>   50.000F    122.000C
50.000C     10.000F <>   50.000F    122.000C

My expected outcome should be:
30.000C   -1.111F <> 30.000C     86.000F
35.000C    1.667F <> 35.000C     95.000F
40.000C    4.444F <> 40.000C    104.000F
45.000C    7.222F <> 45.000C    113.000F
50.000C    10.000F<> 50.000C    122.000F

CodePudding user response:

As suggested by @user16320675, I combined everything into a single for-loop and it is now working as expected! I didn't even know you could combine for-loop statements with the && operator. User's suggestion that answered my problem was:

for (c1=startC, f2=startF; c1<=finishC && f2<=finishF; c1 =stepC, f2 =stepF)

I just made a couple tiny changes and off it went! Thank you everyone for your help and time!

CodePudding user response:

As mentioned in the comments, both loops are working correctly, however, the value of f1 in the outer loop only changes once every 10 cycles of the inner loop (Not every cycle like you want in your example). Remember that the complete inner loop needs to finish all 10 cycles before the outer loop can progress one cycle.

The simple solution is to just use a single loop:

for (c1 = start; c1 <= finish; c1  = five) {                
    f1 = CF(c1);
    c2 = FC(c1);
    System.out.print("\n");
    System.out.format("%6.3fC %6.3fF <> %6.3fF %6.3fC", c1, c2, c1, f1);
}

The outputs will be similar to what you expect. If the conversion is wrong then it indicates an issue with your CF and FC methods.

  • Related