Decision Making and Looping Structures Visual Basic NET

Decision Making and Looping Structures Visual Basic. NET D. Sheefa Ruby Grace 1

Objectives l l l l 2 Plan the solution to a programming problem. Understand apply the rules of logic to complex True/False expressions. Write advanced lf-Then-Else End If statements. Write Do-While and Do-Until statements. Create program loops that depend on logical expressions. Declare and use arrays. Use built‑in functions for a variety of tasks.

Decision Making The simplest version of the If statement takes only a single line: If condition Then statement Endif 3

Simple If - Example The simplest version of the If statement takes only a single line: If (number>0) Then Console. writeline(“Positive Number”) End if 4

Boolean or Logical Expression An expression that, when evaluated, results in a True or False answer. If the condition tested is True, the statement or statements following Then are executed. If the condition tested is False, the statement(s) after Then are skipped. In either case, the statements following the If statement are executed. 5

If … Else Statement If condition Then Statement(s) of the True branch Else Statement(s) of the False branch End If 6

If … Else Statement Example If (Numbers>0) Then Console. writeline(“Positive Number”) Else Console. writeline(“Negative Number”) End If 7

Nesting If Statements If Age < 2 Then Person = "Infant" Else If Age <= 5 Then Person = "Toddler" Else Person = "Older Child" End If 8

Else. If If Age < 2 Then Person = "Infant" Else. If Age <= 5 Then Person = "Toddler" Else Person = "Older Child" End If 9

Three Ifs If Age < 2 Then Person = "Infant" If 2 <= Age And Age <= 5 Then Person = "Toddler" If Age > 5 Then Person = "Older Child" 10

Select Case Statement Select Case textexpression Case expressionlist Statement(s) Case Else Statement(s) End Select 11

Simple Example Select Case day Case 1 console. writeline(“Monday”) Case 2 console. writeline(“Tuesday”) Case Else console. writeline(“Invalid Choice”) End Select 12

Ranges Select Case Age Case Is > 60 grade = “I" Case Is > 50 grade = “II" Case Is > 40 grade = “III" Case else grade = “Nil“ End Select 13

Looping Structures 14 l While. . End While l Do. . Loop l For. . Next

While… End while While condition Statements End while 15

Do While Example In the loop below, the value of i is initialized to 1 before the loop starts. The condition i < 10 is evaluated before the body of the loop is executed. Since the condition is True, the statement in the body of the loop is executed. The value of i is added to sum. The Loop statement at the end sends the program flow back to the top of the loop. At the top of the loop, the condition is tested again to see if it is True. Since it is still True, the loop repeats. 16 i=1 Do While i < 10 sum=sum+i i=i+1 Loop

An Example i=1 Do sum=sum+I i=I+1 Loop While i > 10 17

Do-Until 18 Do Until condition Body Loop Example: i=1 Do Until i > 10 sum=sum+i Loop

Looping Structures For loops are called definite loops because the starting point, the upper limit, and the increment are all known before the loop begins. But it is not always possible to know the number of repetitions necessary for a loop before the loop begins. You may need to process records from a database until you run out of data. Or you may need to process data from the keyboard until a special ending value, called a sentinel value, is entered. For these situations, use an indefinite loop. 19
![For-Next For indexvble=initial to final step [stepsize] Body Next * Initial value should be For-Next For indexvble=initial to final step [stepsize] Body Next * Initial value should be](http://slidetodoc.com/presentation_image_h2/4f8057b49a6e2a0928837ba89a046562/image-20.jpg)
For-Next For indexvble=initial to final step [stepsize] Body Next * Initial value should be less than final value * Default step size is 1 20

For-Next Example For i=1 to 10 sum=sum+i next 21
- Slides: 21