Sei sulla pagina 1di 18

Control Structures in vb.

net
A control structure is a programming structure that allows your program to make decisions. Types of controls

There are three major types of controls structures


1. Conditional logic 2. Looping logic 3. Branching logic

Conditional logic allows you to match condition if condition matches then result come if condition not matches result not come

Example
In job selection form if age is less then 27 then you fill form or if age is more then 27 then you are not eligible for this job.

1. If else end if statement


2. If then statement

3. If then elseif statement


4. Select case statement

Dim Result

if Result >= 57 then label1.text=PASS else label1.text=fail end if

Dim Result if Result >= 75 then label1.text="Grade A " elseif Result >= 60 then label1.text="Grade B " elseif Result >= 45 then label1.text="Grade C " else label1.text="Failed " end if

Dim Result

if Result >= 75 then if Result >= 65 then if Result >= 55 then if Result >= 45 then if Result <= 35 then

label1.text("Grade A ") label1.text("Grade b") label1.text("Grade c ") label1.text("Grade d ") label1.text("Grade fail ")

Dim Flower Flower = "rose"

select case flower case "rose" label1.text(flower & " costs 20rs") case "daisy" label1.text(flower & " costs 15rs") case "orchild" label1.text(flower & " costs 12rs") case else label1.text("There is no such flower in our shop") end select

ASP performs several types of repetitive operations, called "looping". Loops are set of instructions used to repeat the same block of code till a specified condition returns false or true

depending on how you need it.


To control the loops you can use counter variable that increments or decrements with each repetition of the loop.

The For ... Next Loop


The For Each ... Next Loop

The Do ... Loop


The Exit Keyword

Dim i For i = 0 to 10 listbox1.items.add(i) Next

<%@ language="vbscript" %> <% label1.text("<h1>Multiplication table</h1>") label1.text("<table border=2 width=50%") For i = 1 to 9 'this is the outer loop label1.text("<tr>") label1.text("<td>" & i & "</td>") For j = 2 to 9 'inner loop label1.text("<td>" & i * j & "</td>") Next label1.text("</tr>") Next label1.text("</table>") %>

<% Dim i i=0 Do While i<=10 response. write( i & "<br>") i=i+1 Loop %>

<% StartVal=1 Do while StartVal < 11 label1.text "This is loop " & StartVal & "<br>" StartVal=StartVal+1 Loop %>

<% StartVal=1 Do until StartVal = 11 label1.text "This is loop " & StartVal & "<br>" StartVal=StartVal+1 Loop %>

<% StartVal=1 Do until StartVal = 11 If StartVal < 6 then label1.text "This is loop " & StartVal & "<br>" StartVal=StartVal+1 Else Exit Do End If Loop %>

Functions and Sub procedures Functions and sub procedures have many similarities: They can be called or invoked from anywhere within your script. A function or sub procedure can be reused as many times as required, thus saving you time writing out repetitive code Both are blocks of self contained code that can accept arguments. Both will only run when called or invoked from code elsewhere and will not run automatically when the page is loaded.

The difference between a function and a sub is that a sub will do some stuff (like printing something to the screen) and then quit,
while a function runs some code and then returns the result back to the code that called it. A Function could be used to do a calculation and then return a result.

<% Sub NameOfSubRoutine(parameter1, parameter2) 'code goes here... End Sub %>

<% Function NameOfFunction(parameter1, parameter2) 'code goes here NameOfFunction = "return value" End Function %>

<% Sub PrintOut(name,address,country) label1.text name label1.text address label1.text country End Sub %>

<% Call PrintOut("michael","34 Hark Avenue","Ireland") %>

<% Function Total(firstnumber,secondnumber) Total=(firstnumber+secondnumber) End Function %>

<% Sum=Total(2,100) %>

<% label1.text Sum %>

Potrebbero piacerti anche