How to exit while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have the following code:
Section1Length = 3000
Section2Length = 2750
Section3Length = 1000
i = 1
j = 1
k = 1
While i <= Floor(Length / Section2Length)
LengthA = Length - i * Section2Length
While j <= Floor(LengthA / Section3Length)
LengthB = LengthA - j * Section3Length
While k <= Floor(LengthB / Section1Length)
LengthC = LengthB - k * Section1Length
If LengthC <= 0 Then
Exit While
End If
k = k + 1
End While 'While k
LengthB = LengthB - (k - 1) * Section1Length
Messagebox.Show("LengthB="&LengthB)
If LengthB <= 0 Then
Messagebox.Show("Exiting loop j.")
Exit While
End If
j = j + 1
End While 'While j
j = j - 1
LengthA = LengthA - (j - 1) * Section3Length
If LengthA <= 0 Then
Exit While
End If
i = i + 1
End While 'While iWhat I need is this:
I want to solve a Cut-Stock problem. I want to go through all possible options of the three section lengths and then compare the results to get the best solution. The code above is a first test where Length = 9500. This means that the outcome of my code should be k = 1, j =1, i = 2 (Qty3000 = 1, Qty1000 = 1, Qty2750 = 2 --> 1 * 3000 + 1 * 1000 + 2 * 2750 = 9500).
Other variations of the code above would be:
- 1000, 2750, 3000
- 1000, 3000, 2750
- 2750, 3000, 1000
- 3000, 2750, 1000
- 3000, 1000, 2750
The problem seems to be that While j loop is exiting (message "Still in loop j" stops from appearing), but after that I still get messages of the value of LengthB. If Exit While is executed, I would expect not to get any message from LengthB. What am I doing wrong or am I understanding it wrong?