1 / 15

CONTROL STRUCTURE

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.

adin
Télécharger la présentation

CONTROL STRUCTURE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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:

  8. 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

  9. 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

  10. OPERATORS Arithmetical Operators Relational Operators Logical Operators Boolean operator

  11. Arithmetical Operators

  12. Relational Operators

  13. Logical OperatorsIn the Logical operators ,always check the satement is true or false • Boolean operator • True • false

  14. 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

  15. 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

More Related