Control structure--
प्रत्येक prog lang मे program को नियंत्रित करने की सुविधा होती है |जिसके लिए एक विशेष प्रकार की स्ततेमेंट्स का उपयोग किया जाता है जो
प्रोग्राम के flow of execution को नियंत्रित करत्| है वीबी मे इसके लिए कंट्रोल फ़्लो स्ततेमेंट्स का उपयोग किया जाता है
कंट्रोल फलो स्ततेमेंट्स को मुख्यत दो भागो मे विभाजित किया गया है
1) 1- Conditional statements
2) 2- Looping statements
Conditional
statements के अंतर्गत निम्न स्ततेमेंट्स आते है
1)
If
Statement
2) If then else Statement
3) Nested if Statement
4) Ladder else if Statement
5) Select case Statement
1)If
Statement:-यह
प्रोग्राम के फ्लो को सिर्फ एक ही डाइरैक्शन मे फ्लो करता है अर्थात if के साथ कंडिशन लगाया जाता है अगर if
मे दिया गया कंडिशन सही होता है तभी प्रोग्राम का flow of
execution होगा तथा if का स्टेटमेंट execute
होगा | if के साथ end
if का होना आवश्यक है
Syntax of If Statement
If(condition) then
Statement
End if
Example
Dim a As Integer
a = InputBox("Enter Any
Number")
If a > 18 Then
MsgBox ("You are eligiable for
vote")
End If
2)If then else Statement:-यह प्रोग्राम के फ्लो को दोनों डाइरैक्शन मे फ्लो करता है अर्थात if के साथ कंडिशन लगाया जाता है अगर if मे दिया गया कंडिशन सही होता है तभी प्रोग्राम का flow of execution होगा तथा if का स्टेटमेंट execute होगा और अगर कंडिशन false होता है तो else block का स्टेटमेंट execute होता है| if के साथ end if का होना आवश्यक है
Syntax of If then
else Statement
If(condition) then
Statement
Else
statement
End if
Prog:-Write a prog to check the
person in india can vote or not using if else
Dim a As Integer
a = InputBox("Enter Any
Number")
If a > =18 Then
MsgBox ("You are eligiable for
vote")
Else
MsgBox ("You are not eligiable for vote")
End If
Prog:-Write a prog to check enter
number is even or odd using if else
Private Sub Command5_Click()
Dim num As Integer
num = Val(Text1.Text)
If num Mod 2 = 0 Then
Label2.Caption = "Enter Number Is Even"
Else
Label2.Caption = "Enter Number Is odd"
End If
Text1 = ""
Text1.SetFocus
End Sub
Output:-
1) Nested if Statement:-जिस स्टेटमेंट मे प्रोग्राम के फ्लो को कंट्रोल करने के लिए एक से अधिक if टतेमेंट का उपयोग होता है वह नेस्टेड
if else स्टेटमेंट कहलाता है इस स्टेटमेंट का कोई specific
स्ट्रक्चर या syntax नहीं होता है
Syntax of nested If
then else Statement
If(condition) then
If(condition) then
Statement
Else
statement
End if
Else
If(condition) then
Statement
Else
statement
End if
Prog:-Write
a prog to check the person in india can vote or not using bested if else
Private Sub Command6_Click()
Dim age As Integer
Dim name As String
Text2.Text = InputBox("Enter
Age")
Text3.Text = InputBox("Enter
name")
Dim x As Integer
Dim y As String
x = Val(Text2.text)
y = Text3.Text
If x >= 18 Then
If y = "india" Then
MsgBox ("You are
eligiable")
Else
MsgBox ("You are Not
eligiable")
End If
Else
MsgBox ("You are Not
eligiable")
End If
Text2 = ""
Text3 = ""
End Sub
Prog:-Find the greatest number
between three number using nested if else
Dim a, b, c As Integer
a = InputBox("Enter First
Number")
b = InputBox("Enter Second
Number")
c = InputBox("Enter Third
Number")
If a > b Then
If a > c Then
MsgBox ("a is big number")
Else
MsgBox ("c is big number")
End If
Else
If b > c Then
MsgBox ("b is big number")
Else
MsgBox ("c is big number")
End If
End If
1) Ladder else if Statement:-इस स्टेटमेंट मे हम if तथा else के बीच कई elseif स्टेटमेंट
का उपयोग कर कंडिशन चेक करा सकते है जिस if अथवा elseif
मे दिया गया कंडिशन true पाया जाता है उस
ब्लॉक का स्टेटमेंट एक्सेकुटे होता है अगर किसी भी elseif ब्लॉक
का कंडिशन ट्रू न हो तो डिफ़ाल्ट else
ब्लॉक का स्टेटमेंट execute होता है इसमे number
of elseif की संख्या निश्चित नहीं होती है यह प[रोगरम की आवश्यकता
के अनुसार निश्चित किया जाता है
Syntax
If
cond1 then
Statement1
Elseif
cond2 then
Statement2
Elseif
cond3 then
Statement3
Elseif
cond-n then
Statement-n
Else
Default statement
End
if
Example :-
प्रोग:-write a
program to check the enter character is vowel or consonent
Private
Sub Command2_Click()
Dim str
As String
str =
Text4.Text
If str
= "a" Or str = "A" Then
MsgBox
("Enter Character is Vowel")
ElseIf
str = "e" Or str = "E" Then
MsgBox
("Enter Character is Vowel ")
ElseIf
str = "i" Or str = "I" Then
MsgBox
("Enter Character is Vowel ")
ElseIf
str = "o" Or str = "O" Then
MsgBox
("Enter Character is Vowel ")
ElseIf
str = "u" Or str = "U" Then
MsgBox
("Enter Character is Vowel ")
Else
MsgBox
("Enter Character is consonent")
End If
End Sub
Output:-
प्रोग:-write a
program to check the enter number is negative positive or zero
dim num As Integer
num = Val(Text4.Text)
If num > 0
Then
MsgBox("Enter Number Is Positive")
Text4.Text
= ""
Text4.setFocus
ElseIf num = 0 Then
MsgBox("Enter Number Is Zero")
Text4.Text
= ""
Text4.setFocus
Else
MsgBox("Enter Number Is Negaive")
Text4.Text
= ""
Text4.setFocus
End If
End Sub
प्रोग:-write a program
to check profit and loss usin else if
Private
Sub Button7_Click
Dim pp, sp, ans As Integer
pp = Val(Text5.Text)
sp = Val(Text6.Text)
If pp < sp Then
ans = sp – pp
Label8.caption
= "Profit of
following anount " & ans
ElseIf sp < pp Then
ans = pp – sp
Label8.caption = " Loss of following anount " & ans
Else
Label8.caption = " No profit No Loss “
End If
End Sub
1) Select case Statement:- इस स्टेटमेंट का उपयोग भी elseif
ladder स्टेटमेंट की तरह multilevel कंडिशन को
चेक करने के लिए किया जाता है इसमे select के साथ expression
को पास किया जाता है तथा अलग अलग case स्टेटमेंट
मे दिये गए case value के साथ चेक किया जाता है जिस भी case
value के साथ expression match करता है उसी case
का स्टेटमेंट execute होता है इसमे number
of case की संख्या निश्चित नहीं होती है यह प[रोगरम की आवश्यकता के
अनुसार निश्चित किया जाता है select case end select के साथ close
होता है तथा इसका डिफ़ाल्ट स्टेटमेंट case else होता है अर्थात किसी भी case वैल्यू के साथ अगर expression
मैच न हो तो case else का block
execute होता है
Syntax :-
Select case (Expression)
Case (cond-1)
Statement-1
Case (cond-2)
Statement-2
Case (cond-3)
Statement-3
Case (cond-4)
Statement-4
Case (cond-n)
Statement-n
Case else
Statement
End Select
Example
:- write a program to
check the enter character is vowel or consonent
Dim ans As String
ans =
Text5.Text
Select Case
(ans)
Case
"a"
Label2.Caption =
"Enter Character is vowel"
Case Is
= "e"
Label2.Caption =
"Enter Character is vowel"
Case Is
= "i"
Label2.Caption =
"Enter Character is vowel"
Case Is
= "o"
Label2.Caption =
"Enter Character is vowel"
Case Is
= "u"
Label2.Caption =
"Enter Character is vowel"
Case
Else
Label2.Caption = "Enter Character is consonenet"
End Select
Output:-
Prog:- write a program to display the
corresponding day between 0 to 6 using inputbox and msgbox function
Dim a As Integer
a =
InputBox("Enter Any Number")
Select Case
(a)
Case Is
= 1
MsgBox ("Today Is Monday")
Case Is
= 2
MsgBox ("Today Is Tuesday")
Case Is
= 3
MsgBox ("Today Is Wednesday")
Case Is
= 4
MsgBox ("Today Is Thursday")
Case Is = 5
MsgBox
("Today Is Friday")
Case Is
= 6
MsgBox ("Today Is Saturday")
Case
Is = 6
MsgBox ("Today Is Sunday")
Case
Else
MsgBox
("Day Is Not Available")
End Select
No comments:
Post a Comment