If...Then....Else-For....Next

Tips >> ASP

 


The If....Then...Else instructions sequence is very similar to the one we may find in different kind of scripting languages. Let's check an example :-

 

Code :-
<%
AA="water"
If AA="water" Then
response.write ("I want to drink water")
Else
response.write ("I want to drink milk")
End If
%>

 

We may use it this way :-

<% AA="water"
If AA="water" Then %>
I want to drink water

<% Else %>

I want to drink milk

<% End If %>

 

In both cases we have checked a condition (AA="water"), and we have get a positive instruction (to write the sentence "I want to drink water"). We are allowed to execute any kind of instructions (including If....then....Else) and as many instructions as we want.

For....Next

This instructions is also similar in different programming languages. Let's see a typical example.

Code :-
example.asp Try !
I want to say "Hello" 10 times<BR>
<% For mynumber = 1 to 10 %>
<% =mynumber %> Hello<BR>
<% Next %>

 

In this case we have defined a variable ("mynumber") and using the For...Next instruction we have repeated 10 times line 4. Similarly to If....Then....Else instruction, we are allowed to execute any kind of instructions and as many of them as we want.

The For...Next instruction allows to define the value of the increment.

Example#1 :-

Code :-
<% For mynumber = 1 to 20 STEP 2
response.write("Hello<BR>")
Next %>

 

Example#2 :-

Code :-
<% For mynumber = 20 to 1 STEP -2
response.write("Hello<BR>")
Next %>

 

In both cases we will get the same response ("Hello" 10 times). The increment may be positive or negative as shown in the example.

 

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

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641