Posted by: wasaa | June 22, 2008

Control Structures

 

 

Control structures allow you to control the flow of your program’s execution.

If left unchecked by control-flow statements, a program’s logic will flow through statements from left to right, and top to bottom.

Sequences

Selection-(Branching)

Repetition-(Looping)

Selection (Branching)

IF

Select Case

Goto

Repetition

For Next

Do While

Do until

IF Statement

Branching statements are used to cause certain actions within a program if a certain condition is met.

Syntax

If <condition > Then statement

If (Balance – Check) < 0 Then Print “You are overdrawn”

If Then and End If blocks

If Then and End If blocks allows multiple statements

:

Syntax

IF <condition> then

statement 1

statement 2

…………….

statement n

end if

Select Case

Select Case testexpression
[Case expressionlist1
[statementblock-1]]
[Case expressionlist2
[statementblock-2]]
.
.
.
[Case Else
[
statementblock-n]]
End Select

Visual Basic provides the Select Case structure as an alternative to If…Then…Else for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If…Then…Else statement, but it makes codes more readable when there are several choices.

The GoTo Statement

Another branching statement. The format is GoTo Label, where Label is a labeled line.

Labeled lines are formed by typing the Label followed by a colon.

Line10:

…..

Goto Line10

For…Next

When you know you must execute statements a specific number of times,

For…Next loop is the better choice.

“For loop” uses a variable called a counter that increases or decreases in value during each repetition of the loop.

For

Statement

statement

counter = start To end [Step increment]
statement………….

Next

[counter]The arguments counter, start, end, and increment are all numeric.

The increment argument can be either positive or negative.

If increment is positive, start must be less than or equal to end or the statements in the loop will not execute.

For i=1 to 100 step 5

If increment is negative, start must be greater than or equal to end for the body of the loop to execute.

For i=100 to 1 step -5

Do while

Do…Loop Use a Do loop to execute a block of statements an indefinite number of times. There are several variations of the Do…Loop statement. but each evaluates a numeric condition to determine whether to continue execution. Do…Loop, the statements execute as long as the condition is true:

 

Do While

condition
statements Loop

When Visual Basic executes this Do loop,

it first tests condition.

If it’s True (nonzero), Visual Basic executes the statements and then goes back to the Do While statement and tests the condition again.

Another variation of the Do…Loop statement

Do
statements

Loop While conditionexecutes the statements first and then tests condition after each execution. This variation guarantees at least one execution of statements:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: