• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Decision Making and Looping

By Amrit Hallan | on Jun 1, 2005 | 0 Comment
ASP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

Decision Making and Looping

A common feature of any programming script/language is, its ability to perform repeat tasks until a condition is satisfied, and, its ability to take decision. After all, there can be no logic if a program cannot decide what to do and what not to do (sounds like a Shakespearean statement).

Decision Making

VBScript too can take if-then-else decisions. An example:

<%
Dim num1, num2
num1=45
num2=33

if num1<num2 then
%>

  <p><%=num1%> is less then <%=num2%></p>

<%
elseif num1=num2 then
%>

  <p><%=num1%> is equal to <%=num2%></p>

<%
else
%>

  <p><%=num1%> is greater than <%=num2%></p>

<%
end if
%>

Side Note: In the above program, we already know the values of num1 and num2, but if a user enters these values using an HTML form, then only this sort of program will be able to tell which value is greater.

We can also check for multiple conditions, for instance, "if this is Jack’s room and Jack is in the room and the room is in his house then Jack is at home." Programmatically:

<%
Dim JacksRoom, RoomOfJacksHouse, JackInRoom, JackAtHome
JacksRoom=true
JackInRoom=true
RoomOfJacksHouse=true

if JacksRoom=true and JackInRoom=true and RoomOfJacksHouse=true then
  JackAtHome=true
Else
  JackAtHome=false
End if
%>

So for JackAtHome to be true, all preceding variables must be true too. So if we put an AND, all the conditions have to satisfy the "if" condition.

If Jack can be at home if even one of the conditions is true, then we can have

<%
if JacksRoom=or JackInRoom=or RoomOfJacksHouse=true then
  JackAtHome=true
End if
%>

In case of an OR, any one of the conditions can satisfy an if structure.

You can see that the if-then-else-end-if statement is almost similar to the real-world decision-making structure, and hence, is quite self-explanatory. Just take care that each if-then-else-end-if is properly indented, so that you can have multiple sub-if-then-else-end-if’s without getting confused or bogged down.

These are also called logical structure, for, if a and b are not equal, then some condition being a surely means that the condition is not b.

Similar to if-then-else-end-if is the Select Case structure, which is much better organized then the if structures. Let’s see how. (Note that whenever we have to insert remarks in the VBScript code, it is preceded by a single-quote.)

<%
‘ Assuming a visitor chooses a particular food recipe to read and assume the value is being stored in a variable called recipe. So we use the Select Case in the following manner:

Select Case recipe
  Case "Egg Curry" ‘ recipe contains "Egg Curry"
    ’ take the appropriate action
  Case "Mango Pie" ‘ recipe contains "Mango Pie"
    ’ take the appropriate action
  Case Else ‘ All other recipes have been checked for but the one left
    ’ take the appropriate action
End Select
%>

Looping

Many a time you have to perform a programming task again and again. For example, the program should keep reading the data rows of a database file until it encounters the end of the file. Or, a shopping cart script should keep adding items to the cart until the user clicks on "checkout" button.

Primarily, in VBScript, there are four loop structures, namely, for…next, do…until…loop, while…wend, and do…while…loop. Let us see them one by one…continued on the next page.

Decision Making and Looping…Continued

The for…next loop is typically used when the loop has to happen according to some number, and not a criteria. For instance,

<%
for i = 1 to 15
%>

  <p>The current value of i is <%=i%></p>

<%
next
%>

But it doesn’t mean the other loop structures cannot handle number-based loops. We can have the above loop with do…until too.

<%
Dim i
i = 1

do until i = 16
%>

  <p>The current value of i is <%=i%></p>

<%
  i = i+1
loop
%>

While loops are somewhat more sophisticated. In while loops, you can decide whether you necessarily want to execute the code within the while loop or not. The below example explains it properly.

<%
‘ The loop executes at least once, even if the condition is not met with.

Dim CarryOn, i
CarryOn=false

i = 0

do

  i = i + 1

loop while CarryOn=true

%>

By the end of the loop, the variable i will have value 1 even when its condition of CarryOn being true is not met with. Again,

<%
‘ The loop executes not even once if the condition is not met with.

Dim CarryOn, i
CarryOn=false

i = 0

do while CarryOn=true

  i = i + 1

loop

‘ another sort of loop

while CarryOn=true

  i = i + 1

wend
%>

In the above example, the variable i will contain 0 (zero) because it is in the beginning itself when it is checked what sort of value CarryOn has.

The usage of different while loop structures depends on your preferences and need and you can use most of the loops in any way.

A working example of if-then-else-end-if and looping combined.

In this example, the program counts how many times a particular alphabet appears in a given string. It also highlights the found alphabet in the string.

<%
Dim LongString, AlphToFind, AlphCounter

‘ Suppose the alphabet to find is e

LongString = "In the heavy downpour, everything was swept away and we had to swim through the rushing currents to reach the next village."
HighlightedString=""
AlphToFind = "e" ‘ Usually, this value can be entered by some user using an online form.

AlphCounter = 0 ‘ Initially it should have zero value.

‘ we’ll use the For…Next loop

for i=1 to len(LongString)

  if mid(LongString, i, 1) = AlphToFind then
    HighlightedString = HighlightedString & "<strong>" & mid(LongString, i, 1) & "</strong>"
    AlphCounter = AlphCounter + 1
  else
    HighlightedString = HighlightedString & mid(LongString, i, 1)
  end if
next
%>

<p>The letter ‘<%=AlphToFind%>’ appears <%=AlphCounter%> times in the long string "<%=HighlightedString%>"</p>

You may notice some new string-handling functions – len() and mid() – but we shall discuss them at the appropriate time.

Share this story:
  • tweet

Author Description

Amrit Hallan is a freelance web developer. You can follow the link below to checkout his website.

No Responses to “Decision Making and Looping”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 0Followers 497Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    June 2013
    M T W T F S S
    « Oct    
     12
    3456789
    10111213141516
    17181920212223
    24252627282930

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.