0% found this document useful (0 votes)
47 views

Chapter No 6: Control Structure

The document discusses various control structures and procedural programming concepts in VB, including: 1) Mathematical operations using variables, arrays to store multiple values, and If/Then conditional statements. 2) Different types of loops like For/Next, Do/While, and For Each to iterate through collections. 3) Subroutines and functions that can be called or return values, and how arguments are passed by value or reference. 4) Variable scope and the differences between local, block-level, and global variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Chapter No 6: Control Structure

The document discusses various control structures and procedural programming concepts in VB, including: 1) Mathematical operations using variables, arrays to store multiple values, and If/Then conditional statements. 2) Different types of loops like For/Next, Do/While, and For Each to iterate through collections. 3) Subroutines and functions that can be called or return values, and how arguments are passed by value or reference. 4) Variable scope and the differences between local, block-level, and global variables.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

CHAPTER NO 6

Control Structure and Procedural Programming

Mathematical operation with the operands

<script language="vb" runat="server"> Sub Page_Load() Dim intEarn As Integer Dim intTax As Integer Dim decTotal As Decimal intEarn = 150 intTax = 23 decTotal = intEarn - ((intEarn/100)*intTax) Display1.Text = decTotal End Sub </script> <html> <head> <title>Declaring Variables</title> </head> <body> Your total earnings after tax are $ <asp:label id="Display1" runat="server" /> </body> </html>

Structured data types Array Declare an array One dimentional Array Dim nameofarray(size) as datatype

Example of an Array
Sub Page_Load() Dim strArrayDetails(2) As String Dim intLoop As Integer strArrayDetails(0) = text1.Text strArrayDetails(1) = text2.Text strArrayDetails(2) = text3.Text Message1.text = CStr(strArrayDetails(0)) Message2.text = CStr(strArrayDetails(1)) Message3.text = CStr(strArrayDetails(2)) End Sub </script>
<html> <head> <title>Text Box Example</title> </head> <body> <asp:label id="message1" runat="server" /> <br /> <asp:label id="message2" runat="server" /> <br /> <asp:label id="message3" runat="server" /> <br /> <form runat="server"> Please enter your name: <asp:textbox id="text1" runat="server" /> <br /><br /> Please enter your address: <asp:textbox id="text2" runat="server" rows=5 textmode="multiline" /> <br /><br /> Please enter your chosen password: <asp:textbox id="text3" runat="server" textmode="password" /> <br /><br /> <input type="Submit"> </form> </body> </html>

dim twodimarray(2,2) as datatype dimarray(0,0)=value dimarray(0,1)=value dimarray(0,2)=value and so on

IfThen structure <script language="vb" runat="server"> Sub Page_Load() Dim theNumber As Integer Dim theGuess As Integer theNumber = int(10 * rnd) + 1 If Page.IsPostBack Then theGuess = Guess.SelectedItem.Value If theGuess > theNumber then Message.Text = "<BR><BR>Guess is too high<BR>Try again it was " _ & theNumber End If If theGuess < theNumber then Message.Text = "<BR><BR>Guess is too low<BR>Try again it was " _ & theNumber End If If theGuess = theNumber then Message.Text = "<BR><BR>Guess is correct!" End If End If End Sub </script>

<html> <head></head> <body> <form runat="server"> What number am I thinking of? <asp:dropdownlist id="Guess" runat="server"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> <asp:listitem>6</asp:listitem> <asp:listitem>7</asp:listitem> <asp:listitem>8</asp:listitem> <asp:listitem>9</asp:listitem> <asp:listitem>10</asp:listitem> </asp:dropdownlist> <br> <br> <input type="submit" value="Submit guess"> <asp:label id="message" runat="server"/> </form> </body> </html>

If ThenElse structure if confirm=Yes then Message.text=This is correct fax numer Else Message.text=please enter the correct Fax number End if

If ThenElse if structure
IfThenElse if If confirm=Fax Then Messsage.text=please Enter your Fax number Else if confirm=Email Then Message.text=please enter your email numher Else if confirm=voicemail Then Message.text=please enter the voice mail number Else Message.text=NO confirmation will be sent. End if

Select case
<script language="vb" runat="server"> Sub Page_Load() If Page.IsPostBack Then Select Case(Destination.SelectedItem.Value) Case "Barcelona": Message.Text = "You selected Spain's lively Catalan city" Case "Oslo": Message.Text = "Experience the majesty of Norway's capital city" Case "Lisbon": Message.Text = "Portugal's famous seaport and cultural hub" Case else Message.Text = "you did not select a destination we travel to" End Select End If End Sub </script> <html> <head></head> <body> <form runat="server"> Select your choice of destination: <br><br> <asp:radiobuttonlist id="destination" runat="server"> <asp:listitem>Barcelona</asp:listitem> <asp:listitem>Oslo</asp:listitem> <asp:listitem>Lisbon</asp:listitem> </asp:radiobuttonlist> <br><br> <input type="submit" value="Submit Choice"> <br><br> <asp:label id="message" runat="server"/> </form> </body> </html>

Looping structure in Detail:


The ForNext Structure For Loopcounter=startvalue to endvalue Loop code here. Next Loopcounter

Example of for loop structure


<script language="vb" runat="server"> Sub Page_load() Dim number As Integer Dim counter As Integer If Page.IsPostBack then number = NumberAttendees.SelectedItem.value Message1.Text = "" For counter = 1 to number Message1.Text = Message1.Text & _ "Attendee Name ___________________<br /><br />" & _ "Attendee Age _________________<br /><br /><hr /><br />" Next counter End If End Sub </script>
<html> <head> <title>Loop Example</title> </head> <body> <form runat="server"> Enter the number of attendees(max 6): <br> <br> <asp:dropdownlist id="numberAttendees" runat="server"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> <asp:listitem>6</asp:listitem> </asp:dropdownlist> <br> <br> <input type="submit"> <br> <br> <asp:label id="message1" runat="server"/> </form> </body> </html>

Second example of fornext structure

Sub Page_load() Dim counter As Integer For counter = 1 to 6 response.write("this is value no " & counter & "") Next counter

The ForEach structure For Each element In group [statement] [Exit for] [Statement] Next [element]

Example of ForEach structure


Sub Page_load() dim arr(2) as integer dim intelement as integer arr(0)=1 arr(1)=3 arr(2)=45 For each intelement in arr response.write(intelement & "<br />") Next End sub

The Do Structure:
The DoUntil /While structure: Do Code here Loop Until /While a condition is True

Example of Dountil/While structure:

<script language="vb" runat="server"> Sub Page_load() Dim diceRoll As Integer Do diceRoll = int(rnd * 6) + 1 Message1.Text = Message1.Text & "Rolled a: " & diceRoll & "<br />" Loop {Until/While} diceroll = 6 End Sub </script> <html> <head> <title>Do Loop Example</title> </head> <body> <asp:label id="message1" runat="server"/> </body> </html>

The Do{While /Until} Structure:


Do {While/Until} condition Code here. Loop

Subroutines and Functions or Modularization:


<Script language=vb runat=server> Sub Page_Sub() Call subroutine End Sub
Subroutine implementation:

Sub add() or
Sub add(no1 as integer,no2 as integer)

Sub Page_load() call add() call add(100,30) End Sub

dim no1 as integer=100 Dim no2 as Integer=200 Dim Result as Integer Result=no1+no2 message1.text=result End Sub

Functions
<script .> Sub Page_Load() dim r as integer response.write("Required Value of Name Function type=" & NameMyfun()& "<br />" ) 'r=Myfun() 'response.write(r) response.write("Required Value of Return Function type=" & ReturnMyfun() & "<br />") response.write("Required Value of As Data type Function type=" & AstypeNameMyfun() & "<br />") End Sub First type of Function Function NameMyfun() NameMyfun=100 end Function Second type of Function Function ReturnMyfun() return 200 end Function Third type of Function Function AstypeNameMyfun() as integer return 400 end Function </script>

Passing argument through function or Subroutines:

Call by value Call by Reference

Call by value
Sub incr(byval number as integer) number=number+1 end sub Sub Page_Load() dim A as integer A=100 incr(A) Message.text=A End sub
Sub incr(byval number as integer) number=number+1 Message.text=number end sub Sub Page_Load() dim A as integer A=100 incr(A) End sub

Call By Reference
Sub incr(byref number as integer) number=number+1 Message.text=number End sub Sub Page_Load() Dim A as integer A=100 incr(A) Message.text=A End sub

Variable Scope
1. Scope refers to the area in which a variable is used. 2. Performance

Local variable:
Sub Procedure_Number1 () Dim Different As string Different=Hello Im Variable one End Sub Sub Procedure_Number2 () Dim different as String Different=Hello I m variable two End Sub Sub Procedure_Number3 () Dim Difference as String Different=Hello I m variable Three End Sub

Block Level Variable


If intchec =true Then Dim strblock As String Strblock=Very Short ,Lived End if Message.text=strblock //cant be used bcoz out
side the block

Global Variable:
<script language=vb runat=server> dim Global as string//this is accessible any where.

You might also like