Pooja - 6 months ago 4
Java Question

# Alternate solution for Nested 'For' loop

``````for(OPV=230;OPV<245;OPV++)
{
for(IKW = 1.3; IKW <= 2.9; IKW += 0.1)
{
for(OKW = 0.01; OKW < 0.50; OKW += 0.01)
{
for(OPI = 0.05; OPI < 1.10; OPI += 0.01)
{
System.out.println( OPV+" "+IKW+" "+OKW+" "+OPI"")
}
}
}
}
``````

I want to print these data together but I don't want to use nested for loops its creating problem and I am not even getting the range of data which I have given in a loop. Is there any solution rather than nested for loop. Please suggest me.
@Andreas you can see the loop is going infinite i dont want that i want the following output:

``````OPV   IKW OKW   OPI
230, 1.3, 0.01, 0.05
231, 1.4, 0.02, 0.06
232, 1.5, 0.03, 0.07
233, 1.6, 0.04, 0.08
``````

after the range will complete one's then it should repeat again
private static final int RECORD_COUNT =5;
I want it should be increase as my record_count will .
Example if i have entered recor_count =5 it should display only 5 records and it should be cumulative

Do you mean like this?

``````for (int i = 0; i < 200; i++) {
int    OPV = 230  + i %  15;        // 230 - 244 by 1
double IKW = 1.3  + i %  17 * 0.1;  // 1.3 - 2.9 by 0.1
double OKW = 0.01 + i %  49 * 0.01; // 0.01 - 0.49 by 0.01
double OPI = 0.05 + i % 105 * 0.01; // 0.05 - 1.09 by 0.01
System.out.printf("%d, %.1f, %.2f, %.2f%n", OPV, IKW, OKW, OPI);
}
``````

See IDEONE for running example.

OUTPUT

``````230, 1.3, 0.01, 0.05
231, 1.4, 0.02, 0.06
232, 1.5, 0.03, 0.07
233, 1.6, 0.04, 0.08
234, 1.7, 0.05, 0.09
235, 1.8, 0.06, 0.10
236, 1.9, 0.07, 0.11
237, 2.0, 0.08, 0.12
238, 2.1, 0.09, 0.13
239, 2.2, 0.10, 0.14
240, 2.3, 0.11, 0.15
241, 2.4, 0.12, 0.16
242, 2.5, 0.13, 0.17
243, 2.6, 0.14, 0.18
244, 2.7, 0.15, 0.19
230, 2.8, 0.16, 0.20
231, 2.9, 0.17, 0.21
232, 1.3, 0.18, 0.22
233, 1.4, 0.19, 0.23
234, 1.5, 0.20, 0.24
235, 1.6, 0.21, 0.25
236, 1.7, 0.22, 0.26
237, 1.8, 0.23, 0.27
238, 1.9, 0.24, 0.28
239, 2.0, 0.25, 0.29
240, 2.1, 0.26, 0.30
241, 2.2, 0.27, 0.31
242, 2.3, 0.28, 0.32
243, 2.4, 0.29, 0.33
244, 2.5, 0.30, 0.34
230, 2.6, 0.31, 0.35
231, 2.7, 0.32, 0.36
232, 2.8, 0.33, 0.37
233, 2.9, 0.34, 0.38
234, 1.3, 0.35, 0.39
235, 1.4, 0.36, 0.40
236, 1.5, 0.37, 0.41
237, 1.6, 0.38, 0.42
238, 1.7, 0.39, 0.43
239, 1.8, 0.40, 0.44
240, 1.9, 0.41, 0.45
241, 2.0, 0.42, 0.46
242, 2.1, 0.43, 0.47
243, 2.2, 0.44, 0.48
244, 2.3, 0.45, 0.49
230, 2.4, 0.46, 0.50
231, 2.5, 0.47, 0.51
232, 2.6, 0.48, 0.52
233, 2.7, 0.49, 0.53
234, 2.8, 0.01, 0.54
235, 2.9, 0.02, 0.55
236, 1.3, 0.03, 0.56
237, 1.4, 0.04, 0.57
238, 1.5, 0.05, 0.58
239, 1.6, 0.06, 0.59
240, 1.7, 0.07, 0.60
241, 1.8, 0.08, 0.61
242, 1.9, 0.09, 0.62
243, 2.0, 0.10, 0.63
244, 2.1, 0.11, 0.64
230, 2.2, 0.12, 0.65
231, 2.3, 0.13, 0.66
232, 2.4, 0.14, 0.67
233, 2.5, 0.15, 0.68
234, 2.6, 0.16, 0.69
235, 2.7, 0.17, 0.70
236, 2.8, 0.18, 0.71
237, 2.9, 0.19, 0.72
238, 1.3, 0.20, 0.73
239, 1.4, 0.21, 0.74
240, 1.5, 0.22, 0.75
241, 1.6, 0.23, 0.76
242, 1.7, 0.24, 0.77
243, 1.8, 0.25, 0.78
244, 1.9, 0.26, 0.79
230, 2.0, 0.27, 0.80
231, 2.1, 0.28, 0.81
232, 2.2, 0.29, 0.82
233, 2.3, 0.30, 0.83
234, 2.4, 0.31, 0.84
235, 2.5, 0.32, 0.85
236, 2.6, 0.33, 0.86
237, 2.7, 0.34, 0.87
238, 2.8, 0.35, 0.88
239, 2.9, 0.36, 0.89
240, 1.3, 0.37, 0.90
241, 1.4, 0.38, 0.91
242, 1.5, 0.39, 0.92
243, 1.6, 0.40, 0.93
244, 1.7, 0.41, 0.94
230, 1.8, 0.42, 0.95
231, 1.9, 0.43, 0.96
232, 2.0, 0.44, 0.97
233, 2.1, 0.45, 0.98
234, 2.2, 0.46, 0.99
235, 2.3, 0.47, 1.00
236, 2.4, 0.48, 1.01
237, 2.5, 0.49, 1.02
238, 2.6, 0.01, 1.03
239, 2.7, 0.02, 1.04
240, 2.8, 0.03, 1.05
241, 2.9, 0.04, 1.06
242, 1.3, 0.05, 1.07
243, 1.4, 0.06, 1.08
244, 1.5, 0.07, 1.09
230, 1.6, 0.08, 0.05
231, 1.7, 0.09, 0.06
232, 1.8, 0.10, 0.07
233, 1.9, 0.11, 0.08
234, 2.0, 0.12, 0.09
235, 2.1, 0.13, 0.10
236, 2.2, 0.14, 0.11
237, 2.3, 0.15, 0.12
238, 2.4, 0.16, 0.13
239, 2.5, 0.17, 0.14
240, 2.6, 0.18, 0.15
241, 2.7, 0.19, 0.16
242, 2.8, 0.20, 0.17
243, 2.9, 0.21, 0.18
244, 1.3, 0.22, 0.19
230, 1.4, 0.23, 0.20
231, 1.5, 0.24, 0.21
232, 1.6, 0.25, 0.22
233, 1.7, 0.26, 0.23
234, 1.8, 0.27, 0.24
235, 1.9, 0.28, 0.25
236, 2.0, 0.29, 0.26
237, 2.1, 0.30, 0.27
238, 2.2, 0.31, 0.28
239, 2.3, 0.32, 0.29
240, 2.4, 0.33, 0.30
241, 2.5, 0.34, 0.31
242, 2.6, 0.35, 0.32
243, 2.7, 0.36, 0.33
244, 2.8, 0.37, 0.34
230, 2.9, 0.38, 0.35
231, 1.3, 0.39, 0.36
232, 1.4, 0.40, 0.37
233, 1.5, 0.41, 0.38
234, 1.6, 0.42, 0.39
235, 1.7, 0.43, 0.40
236, 1.8, 0.44, 0.41
237, 1.9, 0.45, 0.42
238, 2.0, 0.46, 0.43
239, 2.1, 0.47, 0.44
240, 2.2, 0.48, 0.45
241, 2.3, 0.49, 0.46
242, 2.4, 0.01, 0.47
243, 2.5, 0.02, 0.48
244, 2.6, 0.03, 0.49
230, 2.7, 0.04, 0.50
231, 2.8, 0.05, 0.51
232, 2.9, 0.06, 0.52
233, 1.3, 0.07, 0.53
234, 1.4, 0.08, 0.54
235, 1.5, 0.09, 0.55
236, 1.6, 0.10, 0.56
237, 1.7, 0.11, 0.57
238, 1.8, 0.12, 0.58
239, 1.9, 0.13, 0.59
240, 2.0, 0.14, 0.60
241, 2.1, 0.15, 0.61
242, 2.2, 0.16, 0.62
243, 2.3, 0.17, 0.63
244, 2.4, 0.18, 0.64
230, 2.5, 0.19, 0.65
231, 2.6, 0.20, 0.66
232, 2.7, 0.21, 0.67
233, 2.8, 0.22, 0.68
234, 2.9, 0.23, 0.69
235, 1.3, 0.24, 0.70
236, 1.4, 0.25, 0.71
237, 1.5, 0.26, 0.72
238, 1.6, 0.27, 0.73
239, 1.7, 0.28, 0.74
240, 1.8, 0.29, 0.75
241, 1.9, 0.30, 0.76
242, 2.0, 0.31, 0.77
243, 2.1, 0.32, 0.78
244, 2.2, 0.33, 0.79
230, 2.3, 0.34, 0.80
231, 2.4, 0.35, 0.81
232, 2.5, 0.36, 0.82
233, 2.6, 0.37, 0.83
234, 2.7, 0.38, 0.84
235, 2.8, 0.39, 0.85
236, 2.9, 0.40, 0.86
237, 1.3, 0.41, 0.87
238, 1.4, 0.42, 0.88
239, 1.5, 0.43, 0.89
240, 1.6, 0.44, 0.90
241, 1.7, 0.45, 0.91
242, 1.8, 0.46, 0.92
243, 1.9, 0.47, 0.93
244, 2.0, 0.48, 0.94
230, 2.1, 0.49, 0.95
231, 2.2, 0.01, 0.96
232, 2.3, 0.02, 0.97
233, 2.4, 0.03, 0.98
234, 2.5, 0.04, 0.99
``````
Source (Stackoverflow)