Wiki source for KeyPgWhilewend


Show raw source

{{fbdoc item="title" value="WHILE...WEND"}}----
Control flow statement for looping

{{fbdoc item="syntax"}}##
**While** [//condition//]
[//statement block//]
**Wend**
##
{{fbdoc item="desc"}}
The ##**While**## statement will cause the following set of statements in the ##//statement block//## to execute repeatedly if and while the expression //condition// evaluates to true.

If ##//condition//## evaluates to false when the ##**While**## statement is first executed, then the ##//statement block//## is skipped and execution resumes immediately following the enclosing ##**Wend**## statement.

If an ##[[KeyPgExit|Exit]] **While**## statement is encountered inside the //statement block//, the loop is terminated, and execution resumes immediately following the enclosing ##**Wend**## statement. If a ##[[KeyPgContinue|Continue]] **While**## statement is encountered, the rest of the ##//statement block//## is skipped and execution resumes at the ##**While**## statement.

Like all control flow statements, the ##**While**## statement can be nested, that is, it can be used in a statement block of another ##[[KeyPgWhile|While]]## statement.

//**note**: the ##**While**## //keyword// is also used in the ##[[KeyPgDoloop|Do...Loop]]## statement to indicate the type of comparison. Used in this way, the ##**Do**## statement becomes functionally equivalent to the ##**While**## statement, so do not confuse their enclosing keywords ##Loop## and ##**Wend**##, respectively.//

{{fbdoc item="ex"}}
In this example, a ##**While**## loop is used to reverse a string by iterating through it backward. The loop stops if index is less than ##0## //(##0## being the first index in the string)//.
{{fbdoc item="filename" value="examples/manual/control/while-wend.bas"}}%%(freebasic)
dim as string sentence '' string to reverse
sentence = "The quick brown fox jumps over the lazy dog."

dim as string ecnetnes
dim as integer index
index = len( sentence ) - 1 '' point to last character
while( index >= 0 ) '' stop after first character
ecnetnes += chr( sentence[index] ) '' append character to new string
index -= 1
wend

print "original: """ ; sentence ; """"
print "reversed: """ ; ecnetnes ; """"

end 0
%%

{{fbdoc item="lang"}}
- In the //[[CompilerOptlang|-lang qb]]// and //[[CompilerOptlang|-lang fblite]]// dialects, variables declared inside a ##**While**##..##**Wend**## loop have a function-wide [[ProPgVariableScope|scope]] as in QB
- In the //[[CompilerOptlang|-lang fb]]// and //[[CompilerOptlang|-lang deprecated]]// dialects, variables declared inside a ##**While**##..##**Wend**## block are visible only inside the block, and can't be accessed outside it.

{{fbdoc item="diff"}}
- None

{{fbdoc item="see"}}
- ##[[KeyPgExit|Exit]]##
- ##[[KeyPgContinue|Continue]]##
- ##[[KeyPgDoloop|Do...Loop]]##

{{fbdoc item="back" value="CatPgControlFlow|Control Flow"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode