While/EndWhile

Description:
The Whiile Flow Control statement allows a set of statements to be executed repeatedly while the specified Condition is true. While Flow Control statements can be placed within other Flow Control statements (nested).

Format:
While <condition>
 <true statements> (optional)
EndWhile

Constructs:

<condition>

A phrase or set of phrases that can be evaluated to either TRUE or FALSE. For more information see Comparing Values.

<true statements>

Any set of Autoweb Statements or Functions to be executed while the Condition is TRUE. This section can be empty.

Usage:
Use this Statement to repeatedly execute other Statements (loop) as long as the Condition is TRUE. The <true statements> are optional but usually needed. The EndWhile is always required. Placing While statements inside of other While statements (nested) is allowed. When AutoWeb encounters a While statement, it will evaluate the Condition. If the Condition is TRUE, the <true statements> between the While and EndWhile are executed and then the Condition is evaluated again to see if the <true statements> should be executed again. If or when the Condition is FALSE, the Statements following the EndWhile are executed. If the Condition never becomes FALSE, the While statement will continue executing forever (an Infinite Loop). Infinite Loops are not generally desirable. Many times While Statements are used in conjunction with Occurrence Counters in Functions. By changing the Occurrence Counter in the While, you can change what happens each time the <true statements> are executed.

Use While Statements to:
Process a list on a document
Process all the links on a document
Download all the Pictures on a page

Example 1:
Open and save each Link on a Document with the word 'Chapter' in it. Use an integer variable to control which Link is opened and saved. The ClickLinkWithText function will return TRUE if the link was present and successfully clicked on.

// This example assumes you have opened a Document with chapter links on it
// Create a Chapter Counter and start it at chapter 1
integer	intChapter
intChapter = 1

// Open each Chapter and save it
// ClickLinkWithText will open the Chapter based on the intChapter counter
// When the Chapter does not exist, the While will stop executing
While ClickLinkWithText ('*Chapter*', intChapter)
	// Chapter is displayed, ask the User if they want to save it
	If MessageBox ('Save Chapter','Save this Chapter?', 'Question', 'Yes, No') = 'YES'
		// Save the Documnet
		SaveDocument('C:\Data\Chapter' + ToString (intChapter) + '.htm')
	EndIf
	// Return to the document with the Chapter links
	GoBackDocument ()
	// Get ready for the next chapter by adding 1 to the Chapter Counter
	intChapter = intChapter + 1
EndWhile

Example 2:
Do the same as Example 1, but do not view the Chapter before saving it. The SaveLinkWithText function will return TRUE if the link was present and successfully saved.

// This example assumes you have opened a Document with chapter links on it
// Create a Chapter Counter and start it at chapter 1
integer	intChapter
intChapter = 1

// Save each Chapter
// SaveLinkWithText will save the Chapter based on the intChapter counter
// When the Chapter does not exist, the While will stop executing
While SaveLinkWithText ('*Chapter*', 'C:\Data\Chapter' + ToString (intChapter) + '.htm', intChapter)
	// Get ready for the next chapter by adding 1 to the Chapter Counter
	intChapter = intChapter + 1
EndWhile

Example 3:
Save each picture on a document. Use an integer variable to control which Link is opened and saved. The GetLinkWithText function will return TRUE if the link was present.

// This example assumes you have opened a Document with picture links on it
// Create a Link Counter and start it at link 1
integer	intLink
intLink = 1

// Get each Link Address and save it if it is a Picture
// GetLinkWithText will return the Linked Document Address based on the intLink counter
// When the Link does not exist, the While will stop executing

strLink = GetLinkWithText ('*', intLink)
While Length(strLink) > 0
	// See if Link is a Picture
	If Pos(strLink, '.jpg') > 0
		// It is a Picture file, save it
		SaveDocument(strLink, 'C:\Data\Picture' + ToString (intLink) + '.jpg')
	EndIf
	// Get the next Link by adding 1 to the Link Counter
	intLink = intLink + 1
	strLink = GetLinkWithText ('*', intLink)
EndWhile
Hosted by www.Geocities.ws

1