Friday, June 10, 2016

The Vb.NET Control flow statements

Decision Making

Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
VB.Net provides following types of decision making statements.

If...Then Statement

It is the simplest form of control statement, frequently used in decision making and changing the control flow of the program execution. Syntax for if-then statement is:
If condition Then
[Statement(s)]
EndIf
Where condition is a Boolean or relational condition and Statement(s) is a simple or compound statement. Example of an If-Then statement is:
If(a <=20)Then
   c= c+1
EndIf
If the condition evaluates to true then the block of code inside the If statement will be executed. If condition evaluates to false then the first set of code after the end of the If statement (after the closing End If) will be executed.

Example:

Module decisions
SubMain()
'local variable definition 
Dim a AsInteger=10
 
' check the boolean condition using if statement 
If(a <20)Then
' if condition is true then print the following 
Console.WriteLine("a is less than 20")
EndIf
Console.WriteLine("value of a is : {0}", a)
Console.ReadLine()
EndSub
EndModule


When the above code is compiled and executed, it produces following result:
a is less than 20
value of a is : 10

Select Case Statement

A Select Case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each select case.

Syntax:

The syntax for a Select Case statement in VB.Net is as follows:
Select[Case] expression
[Caseexpressionlist
[ statements]]
[CaseElse
[elsestatements]]
EndSelect
Where,
·         expression: is an expression that must evaluate to any of the elementary data type in VB.Net, i.e., Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort.
·         expressionlist: List of expression clauses representing match values for expression. Multiple expression clauses are separated by commas.
·         statements: statements following Case that run if the select expression matches any clause in expressionlist.
·         elsestatements: statements following Case Else that run if the select expression does not match any clause in the expressionlist of any of the Case statements.

 

Example:

Module decisions
SubMain()
'local variable definition
Dim grade AsChar
grade="B"
Select grade
Case"A"
Console.WriteLine("Excellent!")
Case"B","C"
Console.WriteLine("Well done")
Case"D"
Console.WriteLine("You passed")
Case"F"
Console.WriteLine("Better try again")
CaseElse
Console.WriteLine("Invalid grade")
EndSelect
Console.WriteLine("Your grade is  {0}", grade)
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
Well done
Your grade is B

 

Loops - Iterations

There may be a situation when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.A loop statement allows us to execute a statement or group of statements multiple times.
VB.Net provides following types of loop to handle looping requirements.

Do Loop

It repeats the enclosed block of statements while a Boolean condition is True or until the condition becomes True. It could be terminated at any time with the Exit Do statement.
The syntax for this loop construct is:
Tutorial content goes here.....
Do{While|Until} condition
[ statements]
[ ContinueDo]
[ statements]
[ExitDo]
[ statements]
Loop
-or-
Do
[ statements]
[ ContinueDo]
[ statements]
[ExitDo]
[ statements]
Loop{While|Until} condition

 

 

Example:

Module loops
SubMain()
' local variable definition 
Dim a AsInteger=10
'do loop execution 
Do
Console.WriteLine("value of a: {0}", a)
          a = a +1
LoopWhile(a <20)
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
The program would behave in same way, if you use an Until statement, instead of While:
Module loops
SubMain()
' local variable definition 
Dim a AsInteger=10
'do loop execution 
Do
Console.WriteLine("value of a: {0}", a)
          a = a +1
LoopUntil(a =20)
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

 

For...Next Loop

It repeats a group of statements a specified number of times and a loop index counts the number of loop iterations as the loop executes.
The syntax for this loop construct is:
For counter [Asdatatype]= start Toend[Stepstep]
[ statements]
[ ContinueFor]
[ statements]
[ExitFor]
[ statements]
Next[ counter]

Example:

Module loops
SubMain()
Dim a AsByte
'for loop execution 
For a =10To20
Console.WriteLine("value of a: {0}", a)
Next
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
If you want to use a step size of 2, for example, you need to display only even numbers, between 10 and 20:
Module loops
SubMain()
Dim a AsByte
' for loop execution 
For a =10To20Step2
Console.WriteLine("value of a: {0}", a)
Next
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 12
value of a: 14
value of a: 16
value of a: 18
value of a: 20

 

For Each...Next Loop

It repeats a group of statements for each element in a collection. This loop is used for accessing and manipulating all elements in an array or a VB.Net collection.
The syntax for this loop construct is:
ForEach element [Asdatatype]In group
[ statements]
[ ContinueFor]
[ statements]
[ExitFor]
[ statements]
Next[ element]

 

Example:

Module loops
SubMain()
DimanArray()AsInteger={1,3,5,7,9}
DimarrayItemAsInteger
'displaying the values
ForEacharrayItemInanArray
Console.WriteLine(arrayItem)
Next
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
1
3
5
7
9

 

While... End While Loop

It executes a series of statements as long as a given condition is True.
The syntax for this loop construct is:
While condition
[ statements]
[ ContinueWhile]
[ statements]
[ExitWhile]
[ statements]
EndWhile
Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is logical true. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Here key point of the While loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example:

Module loops
SubMain()
Dim a AsInteger=10
' while loop execution '
While a <20
Console.WriteLine("value of a: {0}", a)
          a = a +1
EndWhile
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

 

With... End With Statement

It is not exactly a looping construct. It executes a series of statements that repeatedly refers to a single object or structure.
The syntax for this loop construct is:
Withobject
[ statements]
EndWith

 

Example:

Module loops
PublicClass Book
PublicProperty Name AsString
PublicProperty Author AsString
PublicProperty Subject AsString
EndClass
SubMain()
DimaBookAsNew Book
WithaBook
.Name ="VB.Net Programming"
.Author ="Zara Ali"
.Subject ="Information Technology"
EndWith
WithaBook
Console.WriteLine(.Name)
Console.WriteLine(.Author)
Console.WriteLine(.Subject)
EndWith
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
VB.Net Programming
Zara Ali
Information Technology


Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
VB.Net provides the following control statements.

Exit Statement

The Exit statement transfers the control from a procedure or block immediately to the statement following the procedure call or the block definition. It terminates the loop, procedure, try block or the select block from where it is called.
If you are using nested loops ( i.e., one loop inside another loop), the Exit statement will stop the execution of the innermost loop and start executing the next line of code after the block.

Syntax:



Exit{Do|For|Function|Property|Select|Sub|Try|While}

Example:

Module loops
SubMain()
' local variable definition 
Dim a AsInteger=10
' while loop execution '
While(a <20)
Console.WriteLine("value of a: {0}", a)
          a = a +1
If(a >15)Then
'terminate the loop using exit statement 
ExitWhile
EndIf
EndWhile
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

Continue Statement

The Continue statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. It works somewhat like the Exit statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.
For the For...Next loop, Continue statement causes the conditional test and increment portions of the loop to execute. For the While and Do...While loops, continue statement causes the program control passes to the conditional tests.

Syntax:

The syntax for a Continue statement is as follows:
Continue {Do|For|While}

Example:

Module loops
SubMain()
' local variable definition 
Dim a AsInteger=10
Do
If(a =15)Then
' skip the iteration '
              a = a +1
              Continue Do
EndIf
Console.WriteLine("value of a: {0}", a)
          a = a +1
LoopWhile(a <20)
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

 

GoTo Statement

The GoTo statement transfer controls unconditionally to a specified line in a procedure.
The syntax for the GoTo statement is:
GoTo label

Example:

Module loops
SubMain()
' local variable definition 
Dim a AsInteger=10
Line1:
Do
If(a =15)Then
' skip the iteration '
              a = a +1
GoTo Line1
EndIf
Console.WriteLine("value of a: {0}", a)
          a = a +1
LoopWhile(a <20)
Console.ReadLine()
EndSub
EndModule
When the above code is compiled and executed, it produces following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19



Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home