vb6(branching,loops) .ppt

25
Friday, May 6, 202 2 1 VISUAL BASIC 6 (cont.) 1. BRANCHING 2. LOOPS

Upload: elly-stephen

Post on 14-Apr-2015

265 views

Category:

Documents


24 download

DESCRIPTION

VB Loops

TRANSCRIPT

Page 1: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 1

VISUAL BASIC 6 (cont.)

1. BRANCHING

2. LOOPS

Page 2: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 2

BRANCHING• If …Then Statement• Syntax:

If (condition) Then

(reaction)

End If

If there is only one reaction to the condition, the statement can also be expressed without the End If

Page 3: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 3

If..Then..Else Statement • Syntax: If condition Then statement Else other statement End If If the original condition is met, then all the code

within the first statement is executed. The optional Else section specifies an alternative statement that will be executed if the condition is false.

Page 4: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 4

• The If-Else statement can be extended to the following form: (called nested if)

If condition Then statement ElseIf condition Then other statement ElseIf condition Then other statement ... Else condition Then another statement End If

Page 5: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 5

Select Case • If you have a lot of conditional statements,

using If..Then..Else could be very messy. For multiple conditional statements, it is better to use Select Case

• The data type specified in expression must match that of Case values. (see syntax below)

Page 6: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 6

Select Case expression   Case value1

        Block of one or more VB statements    Case value2         Block of one or more VB Statements    Case value3         .         .         .    Case Else         Block of one or more VB Statements

End Select

Page 7: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 7

• Example 1: Select Case grade   Case  "A"

       result.Caption="High Distinction"   Case “B+"       result.Caption="Distinction"   Case "B"         result.Caption="Credit"   Case "C"         result.Caption="Pass"   Case Else         result.Caption="Fail"  End Select

• *Please note that grade is a string, so all the case values such as "A" are of String data type.

Page 8: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 8

• Example 2:

Select Case mark

  Case Is >= 85 comment.Caption = "Excellence"

Case Is >= 70   comment.Caption = "Good"

Case Is >= 60

   comment.Caption = "Above Average"

Page 9: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 9

Case Is >= 50

comment.Caption = "Average"

Case Else

comment.Caption = "Need to work harder"

End Select

• * Note we use the keyword Is here to impose the conditions. This is generally used for numeric data.

Page 10: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 10

LOOPS

• Visual Basic allows a procedure to be repeated as many times as long as the processor could support. This is generally called  looping

Visual Basic has two main types of loops: (i) for… next loops (ii) Do… loops

Page 11: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 11

For..Next Loops

• The syntax of a For..Next loop has three components: a counter, a range, and a step. A basic for..next loop appears as follows:

For counter=start_value to end_value (Step increment)

   One or more VB statements Next

start_value and end_value can be any numbers, variables or expressions that produce suitable value.

Page 12: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 12

• Example: (a)      For  counter=1 to 10              display.Text=counter

       Next (b)      For counter=1 to 1000 step 10          counter=counter+1

        Next (c)       For counter=1000 to 5 step -5           counter=counter-10            Next

Page 13: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 13

Do Loops

• Do loops are a bit more flexible then For loops, but should generally only be used when necessary. Do loops come in the following formats:

• Do while

• Do until

• Loop while

• Loop until

Page 14: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 14

• While loops (both do while and loop while) will continue to execute as long as a certain conditional is true. An Until loop will loop as long as a certain condition is false, on the other hand. The only difference between putting either While or Until in the Do section or the Loop section, is that Do checks when the loop starts, and Loop check when the loops ends.

Page 15: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 15

Endless loop: Do..Loop• The endless loop is a loop which never

ends and the statements inside are repeated forever.

• Syntax:

Do

Do_Something

Loop

Page 16: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 16

Do While…Loop • This loop has a condition at the beginning.

The statements are repeated as long as the condition is met. If the condition is not met at the very beginning then the statements inside the loop are never executed.

Do While condition

        Block of one or more VB statements

  Loop

Page 17: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 17

X =1

Do While X <= 5

Print X

X = X + 1

Loop

Page 18: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 18

Do Until…loop• Syntax: Do Until condition

    Block of one or more VB statements Loop

Example: X =1 Do Until X > 5 Print X X = X + 1 Loop

Page 19: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 19

Do…Loop Until

• This loop has a condition at the end and the statements are repeated until the condition is met. Since the check is at the end the statements are at least executed once

Do    Block of one or more VB statements

Loop Until condition

Page 20: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 20

X =1

Do

Print X

X = X + 1

Loop Until X = 6

Page 21: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 21

Do…Loop While• Syntax: Do

   Block of one or more VB statements Loop While condition

Example X =1 Do Print X X = X + 1 Loop while X <= 5

Page 22: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 22

Do..Exit Do..Loop (loop with condition in the middle )

• Sometimes you need to first make a calculation and exit the loop when a certain criterion is met. However when the criterion is not met there is something else to be done. Hence you need a loop where the exit condition is in the middle

Page 23: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 23

• Syntax:

Do

Block of one or more VB statements

(condition)

Exit Do

(end condition)

Block of one or more VB statements

Loop

Page 24: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 24

• Example:

X = 1Do Print X X = X + 1 If X > 5 Then Exit Do End If Loop

Page 25: VB6(branching,loops) .ppt

Tuesday, April 11, 2023 25

NESTED LOOPS

• This is when one or more loops are inside the other.• They may of the same kind or different. Example: For i = 1 to 10 For j = 1 to 10 k = i*j print k Next j Next i