CONTROL STRUCTURE Control Statements are used to control

  • Slides: 15
Download presentation
CONTROL STRUCTURE Control Statements are used to control the flow of program's execution. Visual

CONTROL STRUCTURE Control Statements are used to control the flow of program's execution. Visual Basic supports control structures such as if. . . Then, if. . . Then. . . Else, Select. . . Case, and Loop structures such as Do While. . . Loop, While. . . Wend, For. . . Next etc method. If. . . Then selection structure The If. . . Then selection structure performs an indicated action only when the condition is True; otherwise the action is skipped. Syntax of the If. . . Then selection If <condition> Then statement End If e. g. : If average>75 Then txt. Grade. Text = "A" End If

 • If. . . Then. . . Else selection structure The If. .

• If. . . Then. . . Else selection structure The If. . . Then. . . Else selection structure allows the programmer to specify that a different action is to be performed when the condition is True than when the condition is False. Syntax of the If. . . Then. . . Else selection If <condition > Then statements Else statements End If e. g. : If average>50 Then txt. Grade. Text = "Pass" Else txt. Grade. Text = "Fail" End If

 • Nested If. . . Then. . . Else selection structures test for

• Nested If. . . Then. . . Else selection structures test for multiple cases by placing If. . . Then. . . Else selection structures inside If. . . Then. . . Else structures. Syntax of the Nested If. . . Then. . . Else selection structure • Method 1 If < condition 1 > Then statements Else. If < condition 2 > Then statements Else. If < condition 3 > Then statements Else Statements End If

 • Method 2 If < condition 1 > Then statements Else If <

• Method 2 If < condition 1 > Then statements Else If < condition 2 > Then statements Else If < condition 3 > Then statements Else Statements End If End. If e. g. : Assume you have to find the grade using nested if and display in a text box

If average > 75 Then txt. Grade. Text = "A" Else. If average >

If average > 75 Then txt. Grade. Text = "A" Else. If average > 65 Then txt. Grade. Text = "B" Else. If average > 55 Then txt. Grade. text = "C" Else. If average > 45 Then txt. Grade. Text = "S" Else txt. Grade. Text = "F" End If • Select. . . Case selection structure Select. . . Case structure is an alternative to If. . . Then. . . Else. If for selectively executing a single block of statements from among multiple block of statements. Select. . . case is more convenient to use than the If. . . Else. . . End If. The following program block illustrate the working of Select. . . Case. • Syntax of the Select. . . Case selection structure Select Case Index Case 0 Statements Case 1 Statements End Select

e. g. : Assume you have to find the grade using select. . .

e. g. : Assume you have to find the grade using select. . . case and display in the text box Dim average as Integer average = txt. Average. Text Select Case average Case 100 To 75 txt. Grade. Text ="A" Case 74 To 65 txt. Grade. Text ="B" Case 64 To 55 txt. Grade. Text ="C" Case 54 To 45 txt. Grade. Text ="S" Case 44 To 0 txt. Grade. Text ="F" Case Else Msg. Box "Invalid average marks" End Select

LOOPS • While. . . Loop Statement While(condition) …………. . wend Loop Eg Dim

LOOPS • While. . . Loop Statement While(condition) …………. . wend Loop Eg Dim I, count as integer While i<= 5 Counter=counter+I Wend Text 1. text=counter Do. . . While Statement The Do. . . Loop While statement first executes the statements and then test the condition after each execution. The following program block illustrates the structure:

Syntax Do ……. Loop while(condition) Eg dim I as integer i=0 Do If i=0

Syntax Do ……. Loop while(condition) Eg dim I as integer i=0 Do If i=0 then Msgbox”welcome” End if i=1 Counter=counter+I If counter>10 then End if Loop while i<=5 tex 1. text=counter

 • The For. . . Next Loop For loop is the one which

• The For. . . Next Loop For loop is the one which we frequently use for developing Syntax For(initialize; condition; Increment) …………. . Loop Eg Dim count as integer For (1 to 10) Count=count+1 Text 1. text=count loop

OPERATORS ØArithmetical Operators ØRelational Operators ØLogical Operators ØBoolean operator

OPERATORS ØArithmetical Operators ØRelational Operators ØLogical Operators ØBoolean operator

Arithmetical Operators Description Example Result + Add 5+5 10 - Substract 10 -5 5

Arithmetical Operators Description Example Result + Add 5+5 10 - Substract 10 -5 5 / Divide 25/5 5 Integer Division 203 6 * Multiply 5*4 20 ^ Exponent (power of) 3^3 27 Mod Remainder of division 20 Mod 6 2 & String concatenation "ATI"&" "&"Kurunegala" "ATI Kurunegala"

 • Relational Operators Description Example Result > Greater than 10>8 True < Less

• Relational Operators Description Example Result > Greater than 10>8 True < Less than 10<8 False >= Greater than or equal to 20>=10 True <= Less than or equal to 10<=20 True <> Not Equal to 5<>4 True = Equal to 5=7 False

 • Logical Operators In the Logical operators , always check the satement is

• Logical Operators In the Logical operators , always check the satement is true or false Operators Description OR Operation will be true if either of the operands is true AND Operation will be true only if both the operands are true • Boolean operator • True • false

procedures • Procedure is nothing but a code which write from private sub to

procedures • Procedure is nothing but a code which write from private sub to end sub. Eg Private sub command 1_click …………… End sub A block of code which is written inside the sub and end sub is called as procedure cannot return values Two types Main() User defined procedure

Functions Instead of sub we use keyword function in the coding Ablock of code

Functions Instead of sub we use keyword function in the coding Ablock of code written inside function and end function is called a function. additionally function can return the values, function can be called any where in the vb application. Eg Private function display() Print”welcome to bcomca” End function Private sub command 1_click() Display() End sub