150 likes | 369 Views
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.
E N D
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> ThenstatementEnd If e.g.: If average>75 ThentxtGrade.Text = "A"End 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 > ThenstatementsElsestatementsEnd If e.g.: If average>50 ThentxtGrade.Text = "Pass"ElsetxtGrade.Text = "Fail"End If
Nested If...Then...Else selection structure 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 > ThenstatementsElseIf < condition 2 > ThenstatementsElseIf < condition 3 > ThenstatementsElseStatementsEnd If
Method 2 If < condition 1 > ThenstatementsElseIf < condition 2 > ThenstatementsElseIf < condition 3 > ThenstatementsElseStatementsEnd IfEnd IfEndIf e.g.: Assume you have to find the grade using nested if and display in a text box
If average > 75 ThentxtGrade.Text = "A"ElseIf average > 65 ThentxtGrade.Text = "B"ElseIf average > 55 ThentxtGrade.text = "C"ElseIf average > 45 ThentxtGrade.Text = "S"ElsetxtGrade.Text = "F"End If • Select...Case selection structure Select...Case structure is an alternative to If...Then...ElseIf 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 IndexCase 0StatementsCase 1StatementsEnd Select
e.g.: Assume you have to find the grade using select...case and display in the text box Dim average as Integeraverage = txtAverage.TextSelect Case averageCase 100 To 75txtGrade.Text ="A"Case 74 To 65 txtGrade.Text ="B"Case 64 To 55txtGrade.Text ="C"Case 54 To 45txtGrade.Text ="S"Case 44 To 0txtGrade.Text ="F"Case ElseMsgBox "Invalid average marks"End Select
LOOPS • While... Loop Statement While(condition) ………….. wend Loop Eg Dim I,count as integer While i<= 5 Counter=counter+I Wend Text1.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 then Msgbox”welcome” End if i=1 Counter=counter+I If counter>10 then End End if Loop while i<=5 tex1.text=counter
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 (1to 10) Count=count+1 Text1.text=count loop
OPERATORS Arithmetical Operators Relational Operators Logical Operators Boolean operator
Logical OperatorsIn the Logical operators ,always check the satement is true or false • Boolean operator • True • false
procedures • Procedure is nothing but a code which write from private sub to end sub . Eg Private sub command1_click …………… ……… End sub A block of code which is written inside the sub and end sub is called as procedure. procedure cannot return values Two types Main() User defined procedure
Functions Instead of sub we use keyword function in the coding Ablockof 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 command1_click() Display() End sub