Online Technical Support | Web+ Examples | Tutorial | Web+ Homepage

Overview

This example demonstrates the flow control capability of Web+®. It uses a while loop to run through all the potential primes in a given range. To test each of these numbers, it uses another while loop to try different factors.


Implementation Files

File Name Type Function
PRIME.WML Template Displays form to input the range from the user
PRIME2.WML Template Checks for primes in the given range.

Prime.wml

<webNoOutput>
<!---
| Description: Examples to illustrate some basic math operations works.
|
| CVS History:
| $Log: prime.wml,v $
| Revision 1.2 2006/04/03 18:08:50 vidyar
| daily commit
|
|
|
--->
<webCall script=#application.FUNCPATH & "/geturl.wml"# destScript="prime.wml" url=#nextURL# byref sessionID="">
<!--- generate text strings --->
<webCall script=#application.FUNCPATH & "/gettext.wml"# textString="demonstrates" result=#demonstrates# byref lang=#client.lang# langFile=#langFile#>
<webCall script=#application.FUNCPATH & "/gettext.wml"# textString="welcome" result=#welcome# byref lang=#client.lang# langFile=#langFile#>
<webCall script=#application.FUNCPATH & "/gettext.wml"# textString="howitworks" result=#howitworks# byref lang=#client.lang# langFile=#langFile#>
<webCall script=#application.FUNCPATH & "/gettext.wml"# textString="lowerbound" result=#lowerbound# byref lang=#client.lang# langFile=#langFile#>
<webCall script=#application.FUNCPATH & "/gettext.wml"# textString="upperbound" result=#upperbound# byref lang=#client.lang# langFile=#langFile#>

</webNoOutput>
<webSet #javascriptFile# = "prime.wml">
<webInclude script=#application.SCRIPTPATH & "/template.wml"#>

Prime2.wml

<!--- Include the page header file containing title, buttons, etc. --->
<webSet #HeaderPageTitle# = "talentsoft Prime Number Generator">
<webSet #HeaderBigTitle# = "Prime Number Generator">
<webSet #dir# = "/prime">
<webInclude Script="#path#/header.wml">
<webSet #CGIPath# = "#path##dir#">


<!--- Include the link to the 'Inside the Gearbox' page --->
<webSet #GearboxLink# = "/primeIns.wml">
<webInclude script = "#path#/gearbox.wml">

<!--- We will use a loop to make a counter run from #low# to #high#.
We will not bother to test any even numbers; we set #ctr# to be the lowest
odd number in the specified range. We consider the number 2, the only even prime, as a special case. If 2
is in the specified range, we print it out now. --->
<webIf #high gte 2#>
<webIf #low lte 2#>
2,
<webSet #ctr# = 3>
<webSet #NumPrimes# = 1>
<webElse>
<webSet #ctr# = #low + 1 - (low % 2)#>
<webSet #NumPrimes# = 0>
</webIf>
</webIf>

<!--- Begin a while loop to test all odd numbers in the given range. --->
<webWhile #(ctr lte high) and (ctr lte 1000)# NoOutput>

<!--- Assume that #ctr# is not composite; then we'll check for odd factors.
We don't need to try even factors, since #ctr# is odd. --->
<webSet #CtrIsComposite# = 0>
<webSet #TestFactor# = 3>
<webWhile #(TestFactor * TestFactor lte Ctr) and not CtrIsComposite# NoOutput>

<!--- Check #ctr# for divisibility by #TestFactor# --->
<webIf #Ctr % TestFactor = 0#>
<webSet #CtrIsComposite# = 1>
</webIf>
<webSet #TestFactor# = #TestFactor + 2#>
</webWhile>

<!--- If #CtrIsComposite# is no longer 0, then we have found a prime. --->
<webIf #CtrIsComposite = 0#>
<webSet #NumPrimes# = #NumPrimes + 1#>
<webPrint>#ctr#,&##32;</webPrint>
</webIf>

<!--- Now we'll check the next odd number in the specified range --->
<webSet #ctr# = #Ctr + 2#>
</webWhile>

<!--- Include a 'dummy' loop which does not contain the NoOutput attribute
to turn off the output suppression we turned on in the loops above --->
<webFor #ctr# = 1 to 0></webFor>

<br>

<webSelectCase #NumPrimes#>
<webCase "0">
<webPrint><font face="Arial, Helvetica" size=-1>There are no primes between #low# and #high#.</font></webPrint>
<webCase "1">
<webPrint><font face="Arial, Helvetica" size=-1>There is one prime between #low# and #high#.</font></webPrint>
<webCaseElse>
<webPrint><font face="Arial, Helvetica" size=-1>There are #NumPrimes# primes between #low# and #high#.</font></webPrint>
</webSelectCase>

<ul>
<li><a href="<webPrint>#CGIPath#</webPrint>/prime.wml">
<font face="Arial, Helvetica" size=-1>Back to the Prime Number Generator</font></a>
</ul>


 

Copyright © 2009 Webplus, Inc. (talentsoft). All Rights Reserved.
webmaster@webplus.com