How to exit while loop

How to exit while loop

gert-leonvanlier
Collaborator Collaborator
1,130 Views
2 Replies
Message 1 of 3

How to exit while loop

gert-leonvanlier
Collaborator
Collaborator

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 i

What 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?

 

0 Likes
1,131 Views
2 Replies
Replies (2)
Message 2 of 3

jjstr8
Collaborator
Collaborator

As you have it, the "While i" loop executes more than once, so your "While j" will go through another iteration.

0 Likes
Message 3 of 3

J-Camper
Advisor
Advisor

You aren't carrying the Length Modification all the way through, and restarting LengthA each time, so the code will run through 3 times with the same result until i>3, in the case of Length = 9500:

'Assuming Length = 9500
Section1Length = 3000 Section2Length = 2750 Section3Length = 1000 i = 1 j = 1 k = 1 While i <= Floor(Length / Section2Length) '3 LengthA = Length - i * Section2Length '9500-1*2750 = 6750 While j <= Floor(LengthA / Section3Length)'6 LengthB = LengthA - j * Section3Length '6750-1*1000 = 5750 While k <= Floor(LengthB / Section1Length)'1 LengthC = LengthB - k * Section1Length '5750-1*3000 = 2750 If LengthC <= 0 Then Exit While End If k = k + 1 ' k=2 End While 'While k LengthB = LengthB - (k - 1) * Section1Length '6750-(2-1)*3000 = 2750 Messagebox.Show("LengthB="&LengthB) If LengthB <= 0 Then Messagebox.Show("Exiting loop j.") Exit While End If j = j + 1 'j=2 End While 'While j j = j - 1 'j=1 LengthA = LengthA - (j - 1) * Section3Length '6750-(1-1)*1000 = 6750 If LengthA <= 0 Then Exit While End If i = i + 1 'i=2 End While 'While i

What you need to do is store the original Length value and follow the decreasing value with a separate variable.  See code below:

 

Dim oLength As Long = 9500 'Change to read desired Value
Dim Section1Length As Long = 3000
Dim Section2Length As Long = 2750
Dim Section3Length As Long = 1000
Dim followLength As Long = oLength

i = 0 'start @ 0 incase one isn't used
j = 0 'start @ 0 incase one isn't used
k = 0 'start @ 0 incase one isn't used

While i <= Floor(followLength / Section2Length) 
	i = i + 1 'increment at the beginning of the loop so it doesn't increment unnecessarily at the end
	followLength = followLength - Section2Length 
	'MessageBox.Show(followLength, "i: " & i)
	
	If j <= Floor(followLength / Section3Length) 
		j = j + 1 'increment at the beginning of the loop so it doesn't increment unnecessarily at the end
		followLength = followLength - Section3Length 
		'MessageBox.Show(followLength, "j: " & j)
		
		If k <= Floor(followLength / Section1Length) 
			k = k + 1 'increment at the beginning of the loop so it doesn't increment unnecessarily at the end
			followLength = followLength - k * Section1Length 
			'MessageBox.Show(followLength, "k: " & k)
			
		End If 'Loop k
		
	End If 'Loop j

End While 'Loop i
'Show results: If followLength <= 0 'Length was divisible by provided Lengths MessageBox.Show("The Length: " & oLength & " is made up of:" & vbCrLf & _ "Length1= " & Section1Length & " for a quantity of: " & k & vbCrLf & _ "Length2= " & Section2Length & " for a quantity of: " & i & vbCrLf & _ "Length3= " & Section3Length & " for a quantity of: " & j) Else 'There is a remainder value MessageBox.Show("The Length: " & oLength & " is made up of:" & vbCrLf & _ "Length1= " & Section1Length & " for a quantity of: " & k & vbCrLf & _ "Length2= " & Section2Length & " for a quantity of: " & i & vbCrLf & _ "Length3= " & Section3Length & " for a quantity of: " & j & vbCrLf & _ "With a remainder of: " & (followLength)) End If

I was also having an issue with the second iteration of i being skipped when j was a While loop.  Switching j and k loops to if statements within in the i While loop is working better.  Your main loop, in this case "i", will be the first section length to increase in quantity.

0 Likes