[go: up one dir, main page]

0% found this document useful (0 votes)
45 views11 pages

L3-Control Statments 2

This document provides an overview of visual basic loops including for, do, and nested loops. It gives examples of how to use each type of loop to iteratively perform calculations like finding the summation or values in a series. Code examples are provided to calculate totals, check for prime numbers, calculate integrals, and estimate trigonometric functions using loops. The document is intended to teach students visual basic looping structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views11 pages

L3-Control Statments 2

This document provides an overview of visual basic loops including for, do, and nested loops. It gives examples of how to use each type of loop to iteratively perform calculations like finding the summation or values in a series. Code examples are provided to calculate totals, check for prime numbers, calculate integrals, and estimate trigonometric functions using loops. The document is intended to teach students visual basic looping structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Ministry of Higher Education and Scientific Research

Southern Technical University


Basrah Engineering Technical College

Visual Basic
For
Students of second class
Department of Environment and Pollution Engineering
Engineering Technical College/Basrah

By

Dr. Ihssan A. Abdulhussain


Assistant Prof.
Department of Environment and Pollution Engineering
Basrah Engineering Technical College

December/2016
Visual Basic allows a procedure to be repeated as many times as long as the
processor and memory could support. This is generally called looping.
Looping is required when we need to process something repetitively until a
certain condition is met. In Visual Basic, we have three types of Loops, they
are

• For.....Next loop,

• Do…..While…. loop

• Do…..Until…... loop

1- For....Next Loop

The format is:

For counter = Start To End Step [Increment]

One or more VB statements

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. If increment is negative, start must be greater than or equal
to end for the body of the loop to execute. If steps isn’t set, then increment
defaults to 1. In executing the For loop, visual basic:

1. Sets counter equal to start.

2. Tests to see if counter is greater than end. If so, visual basic exits the loop
(if increment is negative, visual basic tests to see if counter is less than end).

3. Executes the statements.

4. Increments counter by 1 or by increment, if it’s specified.

5. Repeats steps 2 through 4.


For Example:

1- For I=0 To 10 step 5

Statements

Next I

2- For counter = 100 To 0 Step -5

Statements

Next counter

Example 1: Design a form and write code to find the summation of numbers
(from 0 to 100).

Solution:

Private Sub form_load()

Form1.show

Dim I As Integer, Total As Integer

For I = 0 To 100

Total= Total +I

Next I

Print "Total="; Total

End sub

Example 2: Design a form and write code to find the summation of even
numbers from 0 to 100.

Solution:

Private Sub form_load)(

Form1.show

Dim I As Integer, Total As Integer

For I = 0 To 100 step 2


Total= Total +I

Next I

Print "Total="; Total

End Sub

Example 3: Design a form and write code to find the summation of odd
numbers (from 0 to 100).

Solution:

Private Sub form_load)(

Form1.show

Dim I As Integer, Total As Integer

For I = 0 To 100

If I mod I =1 Then Total= Total +I

Next I

Print "Total="; Total

End Sub

2- Do –Loop:

Use a Do loop to execute a block of statements and indefinite number of


times. There are several variations of Do…Loop statement, but each evaluates
a numeric condition to determine whether to continue execution. In the
following Do..Loop, the statements execute as long as the condition is True.

2-1 Do While ..Loop

The formats are

Do While condition

Block of one or more VB Statement

Loop
When Visual Basic executes this Do..Loop, it first tests condition. If condition
is False, it skips past all the statements. If it’s True, Visual Basic executes the
statements and then goes back to the Do while statement and tests the
condition again. Consequently, the loop can execute any number of times, as
long as condition is True. The statements never execute if initially False.

For Example: Loop counts from 0 to 100.

Dim num As Integer, Total

num = 0

Do While num <= 100

Total=Total +num

num = num + 1

Loop

Print "Total=", Total

2-2 Do…Loop While:


Another variation of the Do..Loop statement executes the statements first and
then tests condition after each execution. This variation guarantees at least
one execution of statements.
The formats are
Do
Block of one or more VB Statement
Loop While condition
For Example: Loop counts from 0 to 100.
Dim num As Integer, Total
num = 0
Do
Total=Total +num
num = num + 1
Loop While num <= 100
Print "Total=", Total
2-3 Do Until ….Loop
Unlike the Do While...Loop repetition structures, the Do Until... Loop
structure tests a condition for falsity. Statements in the body of a Do
Until...Loop are executed repeatedly as long as the loop-continuation test
evaluates to False.
The formats are
Do Until condition
Block of one or more VB Statement
Loop
For Example: Loop counts from 0 to 100.
Dim num As Integer, Total
num = 0
Do until num >100
Total=Total +num
num = num + 1
Loop
Print "Total=", Total
2-4 Do… Loop Until
The formats are
Do
Block of one or more VB Statement
Loop Until condition
For Example: Loop counts from 0 to 100.
Dim num As Integer, Total
num = 0
Do
Total=Total +num
num = num + 1
Loop Until num >100
Print "Total=", Total
3- Exiting Loop:
The exit statement allows you to exit directly from For Loop and Do Loop,
Exit For can appear as many times as needed inside a For loop, and Exit Do
can appear as many times as needed inside a Do loop ( the Exit Do statement
works with all version of the Do Loop syntax). Sometimes the user might
want to get out from the loop before the whole repetitive process is executed;
the command to use is Exit For To exit a For.....Next Loop or Exit Do To
exit a Do… Loop, and you can place the Exit For or Exit Do statement within
the loop; and it is normally used together with the If....Then.....statement.
* Exit Do *Exit For
The format is: The format is:
Do While condition For counter=Start To Endstep (increm.)
Statements Statements
If …Then…Statement: Exit Do If …Then…Statement: Exit For
Statements Statements
Loop Next counter

For its application, you can refer to example:


Private sub Form Load_( )
Form1.show
Dim n as Integer
For n=1 to 10
If n>6 then Exit For
Picture1.Print n
Next
End Sub
Private sub Form Load_( )
Form1.show
Dim x As Integer
X=0
Do While x < 10
Print x
x=x+1
If x = 5 Then
Print "The program is exited at x=5"
Exit Do
End If
Loop
End Sub

4- Nested Loop: The nested loops are the loops that are placed inside each
other. The most inner loop will be executed first, then the outer ones. These
are examples of the nested loops.

Error (Not Possible) Possible


For K=1 to 5 For J=1 to 5
Statement Statement
For I=1 to 5 For I=1 to 5
Statement Statement
Next K Next I
Statement Statement
Next I Next J

Example 4: For a simply supported beam shown in Fig below. By using the
input box statement, enter the value of length of the beam (L), concentrated
load (P), distance (a) from support, modulus of elasticity (E) and moment of
inertia (IG). Write a code program to find the value of deflection at distance
(X) from support, where X increased by (0.01L) from the following equation.
Print the deflection value in separate text box. Designs a form and select all
control object are used.
Solution:
Private Sub Command1_click( )
Dim L, P, E, IG, a, X, Df
L=Val ( Inputbox (“L=”) )
P=Val ( Inputbox (“P=”) )
IG=Val ( Inputbox (“IG=”) )
E= Val ( Inputbox (“E=”) )
a=Val ( Inputbox (“a=”) )
For X=0 To L Step 0.01 *L
If X< a Then
Df=p*X/(6*E*IG)*(3*L*a-3*a^2-X^2)
ElseIf X>=a .AND. X<= L- a Then

Df= p*a/(6*E*IG)*(3*L*X-3*X^2-a^2)
Else
Msgbox" Value of X greater than L-a" : Exit For
EndIf
Picture1.print X; Df
Next X
End Sub

Example 5: Design a form with one command and two text boxes. Enter the
value of integer number (N) in separate text box. Write a code program to
check if the number (N) is a prime Number or not. Display the “It is not a
prime number” or “It is a prime number” in separate text box.
Solution:
Private Sub Command1_Click()
Dim N, D As Single
Dim tag As String
N = Val(Text1.Text)
Select Case N
Case Is < 2
Text2.text = "It is not a prime number"
Case 2
Text2.text = "It is a prime number"
Case Is > 2
D=2
Do
If N / D = Int(N / D) Then
Text2.text = "It is not a prime number"
tag = "Not Prime"
Exit Do
End If
D=D+1
Loop While D <= N - 1
If tag < > "Not Prime" Then
Text2.text = "It is a prime number"
End If
End Select
End Sub

Example 6: Create a Visual Basic Project to find the value of the


following series. 𝜋2/ 6 = 1 + 1/ 22 + 1 /32 + 1 /42 + ⋯
Write the code program so that the number of terms (N) is entered into text
box. Display the result (Pi) in separate text box when click on command (Find
Pi).

Solution: Private Sub Command1_click()


Dim S as double, N , I , T
N=val(text1.text) : S= 0.0
For I=1 To N
T=1 / I^2
S=S+T
Next
Pi=SQR (S*6)
Text2.text=Str (Pi)
End Sub

Example 7: Create a Visual Basic Project to find the value of the following
series.
𝑆in(𝑥) = 𝑥 − 𝑥3/ 3! + 𝑥5/ 5! − 𝑥7/ 7! + ⋯
Write the code program so that the value of angle (X) is entered into text box.
Estimate the value of series (Sin(x)) so that the absolute value of any term is
greater or equal than 10-6. Display the required number of terms (N) which it
used in this series in a separate text box and display the result of series (Sin(x))
in another separate text box.
Solution:
Private Sub Command1_click()
Dim X, Sx, I, J, T, K, N, Fact
X = Val(Text1.Text): X = X * 3.14 / 180
N = 1: K = 1: Sx = 0.
10 Fact = 1
For I = 1 To 2 * N - 1
Fact = Fact * I
Next I
T = X ^ (2 * N - 1) / Fact
If Abs(T) >= 0.000001 Then
Sx = Sx + T * K
K = -K: N = N + 1
GoTo 10
Else
Text2.Text = Str(N)
Text3.Text = (Sx)
End If: End Sub

H.W 1: Create a Visual Basic Project to find the value of the following series.
Cos (𝑥) =1− 𝑥2 / 2!+𝑥4 / 4!−𝑥6 / 6!+⋯
Write the code program so that the value of angle (X) is entered into text box
and the number of terms (N) is entered into input box. Calculate the value of
series and display the result of series (Cos(x)) in another separate text box.
H.W 2: Create a Visual Basic Project to find the value of the following series.
𝑌=1−𝑋3/32+5𝑋7/72− 9𝑋11/92+⋯ 𝑋>0
𝑌=𝑋2/22−3𝑋6/62+ 5𝑋10/102−⋯ 𝑋<0
Write the code program so that the value of (X) is entered into text box.
Estimate the value of series (Y) until the absolute value of any term is less than
10-6. Display the required number of terms (N) which it used in this series in a
separate text box and display the result of series (Y) in another separate text
box.

You might also like