Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

nested loops

i have the below code which contains a nested for loop...

i am writing a sample code which should automatically check for nested loops...

when i give the below code to my application it should say that you had a nested for loop....

how can i do that...


Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
    On Error GoTo Scan_Issue
    Dim p1 As IASLib.IAS_RECORD_1
    Dim p As IAS_RECORD_0
 For Each p In pRoot.Tree.Pages
        p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
        p.Tree.Parent.Foil_Number.Foil_Number_From_Scan =p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
     For Each p In pRoot.Tree.Pages
        p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
        p.Tree.Parent.Foil_Number.Foil_Number_From_Scan =p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
     Next  p    
Next  p
   
Exit Sub

Open in new window

Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Proper way is to parse the language, then build a syntax tree (AST) and analyze the AST
Simpler, hack is to just use regular expressions or recursive descent parsing and for each "For Each" you find, try to identify another "For Each" that begins before the last one ends ("Next")
Avatar of kaufmed
The quick-and-dirty would be to read each line and check for a "For" at the start of it (ignoring whitespace). Count the number of "For"s you receive before getting to a "Next" and if it's greater than one, you have a nested loop. You would need to add conditions for "While" and "Do" if you care about those.

The proper way would be to create a parser which searched through the string looking for loops. A recursive descent parser would probably get you by.
Great minds think alike. ;)
Wow. Those posts are eerily similar. Great minds, eh?    = )
Jinx!
Avatar of UnifiedIS
UnifiedIS

If you are reading line by line in order, look for either "For Each" or "Next" within the line.
use an integer variable to indicate if you are expecting a "Next" or a "For Each", if you find the opposite, you have a nested loop.

When you find a for each, add 1, when you encounter a next, subtract 1
If your value exceeds 1, you have a nested for...next loop.
@UnifiedIS

If you are reading line by line in order, look for either "For Each" or "Next" within the line.

So what happens when you receive the following:
Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
    On Error GoTo Scan_Issue
    Dim p1 As IASLib.IAS_RECORD_1
    Dim p As IAS_RECORD_0

    ' I had this enabled, but it was wrong, but I might use it again, so I didn't delete it
    'For Each p In pRoot.Tree.Pages

    p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
    p.Tree.Parent.Foil_Number.Foil_Number_From_Scan = p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
    For Each p In pRoot.Tree.Pages
        p.IE_COLOR_DETECT.InputImage = p.Scan.OutputImage
        p.Tree.Parent.Foil_Number.Foil_Number_From_Scan = p.Tree.L2Parent.Scan.Level_2_KeyEntry_0
    Next p

    'Next p
Exit Sub

Open in new window

Look for the single quote at the front of the trimmed line and ignore those lines
Avatar of shragi

ASKER

i foudn index of for each and next ... and verified whether index of for each is greater than next ...

but not able to Count the number of "For"s you receive before getting to a "Next" and if it's greater than one
while ((forLoopIndex = TextFromTextArea.indexOf("For Each", forLoopIndex))!= -1)
	{
		forLoopIndex++;
		System.out.println("Search Index for  is:"+forLoopIndex);
		forLoopCounter++;
	
	}
	
	while ((nextIndex = TextFromTextArea.indexOf("Next", nextIndex))!= -1)
	{
		nextIndex++;
		nextCounter++;
		if(forLoopCounter>nextCounter)
		{
			loopDisplay="Code Contains Nested loops";
			out.println(loopDisplay);
			
		}
		else if(forLoopCounter<=nextCounter)
		{
			loopDisplay="Module contains for loop.";
			out.println(loopDisplay);
		}
	
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok. One of those (number 3) isn't valid. Ooops!