L3-Control Statments 2
L3-Control Statments 2
Visual Basic
For
Students of second class
Department of Environment and Pollution Engineering
Engineering Technical College/Basrah
By
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
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:
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).
Statements
Next I
Statements
Next counter
Example 1: Design a form and write code to find the summation of numbers
(from 0 to 100).
Solution:
Form1.show
For I = 0 To 100
Total= Total +I
Next I
End sub
Example 2: Design a form and write code to find the summation of even
numbers from 0 to 100.
Solution:
Form1.show
Next I
End Sub
Example 3: Design a form and write code to find the summation of odd
numbers (from 0 to 100).
Solution:
Form1.show
For I = 0 To 100
Next I
End Sub
2- Do –Loop:
Do While condition
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.
num = 0
Total=Total +num
num = num + 1
Loop
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.
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 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.