Do While Loop - Select.....Case

Tips >> ASP


Again, we will define a condition and one or more instructions :-

Code :-
<%
mynumber=0
Do While mynumber<10
response.write("Hello<HR>")
mynumber=mynumber+1
Loop
%>

In this example the condition is "mynumber<10" and the instructions defines a response text and an increment of the variable "mynumber". In the example, mynumber will be increased until it gets a value of 10. Then the loop will be abandon. Several instruction may be used within the loop.

Do Until....Loop

Quite similar to the previous one, it also includes a condition and one or more instructions:

Code :-
<%
mynumber=0
Do Until mynumber=10
response.write("Hello<HR>")
mynumber=mynumber+1
Loop
%>

In this example the condition is "mynumber=10", so mynumber will increased until it is equal to 10, and then the loop will be abandon.

Let's see an example using this Do Until...Loop :

Code :-
<%
myfirstnumber=0
mysecondnumber=0
Do Until myfirstnumber=15


Do Until mysecondnumber=15
response.write("X")
mysecondnumber=mysecondnumber+1
Loop
Response.write ("<BR>")
myfirstnumber=myfirstnumber+1
mysecondnumber=myfirstnumber

Loop
Response.write ("END")
%>

The result of the script is this one:

XXXXXXXXXXXXXXX
XXXXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXX
XXXXXXXXXXX
XXXXXXXXXX
XXXXXXXXX
XXXXXXXX
XXXXXXX
XXXXXX
XXXXX
XXXX
XXX
XX
X

Select Case....End Select

This is a very useful instruction in case we want to check different values for variable. Lets check an example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%
mynumber=3
Select Case mynumber
Case 1
Response.write ("Number 1")
Case 2
Response.write ("Number 2")
Case 3
Response.write ("Number 3")
Case 4
Response.write ("Number 4")
Case 5
Response.write ("Number 5")
Case Else
Response write ("Mynumber is higher than 5")
End Select
%>

In this example above, we have defined mynumber as 3, so they are executed the instructions following line 8 (in this case only one instruction is executed, but they may be several instructions). Case Else is not necessary.

Let's try a second example :

1
2
3
4
5
6
7
8
9
10
11
12
13
<%
username=request.form("username")
Select Case username
Case "Peter"
Response.write ("Hello, Peter")
Case "John"
Response.write ("Hello, John")
Case "Joe"
Response.write ("Hi, Joe")
Case Else
Response write ("I do not know you")
End Select
%>

Let's see a different example :-

backgroundform.html
<html>
<head><title>Chose background color</title></head>
<form action="backgroundresponse.asp" method="post">
Which color do you prefer to use as your background?
<BR>
<input type="radio" name="kindofcolor" value="defined" checked>
Defined color
<select name="definedcolor">
<option value="#FFFFFF">White</option>
<option value="#FF0000">Red</option>
<option value="#00FF00">Green</option>
<option value="#0000FF">Blue</option>
</select>
<BR>
<input type="radio" name="kindofcolor" value="custom">
Custom color
<input type="text" size="8" name="mycolor"></input>
<BR><input type="Submit" value="Submit"></input>
</form>
</body>
</html>

backgroundresponse.asp
<%
kindofcolor=Request.form("kindofcolor")
Select Case kindofcolor
case "defined"
colorofbackground=Request.form("definedcolor")
Select Case colorofbackground
case "#FFFFFF"
texttoshow="White"
case "#FF0000"
texttoshow="Red"
case "#00FF00"
texttoshow="Green"
case "#0000FF"
texttoshow="Blue"
End select
case "custom"
colorofbackground=Request.form("mycolor")
texttoshow="Custon color"
End select
%>

<html>
<head><title>Chose background color</title></head>
<body bgcolor="<% =colorofbackground %>">
<center>
<H1><% =texttoshow %></H1>
</center>
</form>
</body>
</html>

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641