Download END IF

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Controlling Program Flow – Modules – Creation of EXE files
•
•
•
In general VB executes its program from top to bottom and left to right. This is called
sequence of execution.
If we want to break the sequence of execution, then we will use a statement called
Control statement or Decision making statements.
VB language possesses decision making capabilities and supports the following
statements known as control or decision making statements.
a) Simple IF …THEN .. END IF statement
b) IF … THEN .. ELSE … END IF statement
c) Nested IF…. THEN … ELSE …END IF statement
d) SELECT CASE statement
e) GOTO statement
1) Simple IF … END IF statement
It is one of the conditional control statement.
Syntax :
IF condition THEN
Statement 1;
----Statement n;
END IF
Statement x;
Meaning :
If <condition> is non-zero (true), <statement1 to statement n > are executed.
If < condition > is false <statement x > is executed .
Example:
Write a VB program to find maximum among two numbers
Private Sub Command1_Click ( )
Dim a As Integer
Dim b As Integer
a = Inputbox(“Enter First number “)
b = Inputbox(“Enter Second number “)
IF a > b THEN
MsgBox (“A is biggest “ & a);
END IF
IF b > a THEN
MsgBox (“Bis biggest “ & b);
END IF
End sub
b) IF … THEN … ELSE statement
It is one of the conditional control statement. If we are having two conditions then we
will use IF .. THEN .. ELSE statement.
Syntax :
IF condition
True block Statement (s);
ELSE
False block statement (s);
END IF
Statement x;
Meaning :
If <condition> is non-zero (true), < True block statement (s) > is executed. If < condition>
is false < False block statement (s) > is executed.
Example:
Write a VB program to find maximum among two numbers
Private Sub Command1_Click ( )
Dim a As Integer, b As Integer
a = Inputbox(“Enter First number “)
b = Inputbox(“Enter Second number “)
IF a > b THEN
MsgBox (“A is biggest “ & a);
ELSE
MsgBox (“Bis biggest “ & b);
END IF
End sub :
c) Nested IF … THEN …ELSE statements
It is one of the conditional control statement. If we are having more than two
conditions then we will use nested IF …THEN .. ELES statements.
Syntax :1
IF condition 1 THEN
IF condition 2 THEN
Statement Block 1;
ELSE
Statement Block 2;
END IF
ELSE
Statement 3;
END IF
Statement x;
Meaning :
If the condition 1 is false, the statement 3 will be executed. If the
condition 1 is true, then control goes to test the condition 2, If the condition
2 is true , the statement 1 will be executed otherwise the statement 2 will be
executed and then the control is transferred to the statement x.
Example: To find maximum among three numbers
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
a = InputBox("Enter first")
b = InputBox("Enter second")
c = InputBox("Enter third")
If a > b Then
If a > c Then
MsgBox ("A is biggest" & a)
Else
MsgBox ("C is biggest" & c)
End If
Else
If b > c Then
MsgBox ("B is biggest" & b)
Else
MsgBox ("C is biggest" & c)
End If
End If
End Sub
Syntax :2
IF condition 1 THEN
Statement Block 1;
ELSEIF condition 2 THEN
Statement Block 2;
ELSEIF condition 3 THEN
Statement Block 3;
-------ELSEIF condition n-1 THEN
Statement Block n-1;
ELSE
Statement Block n;
END IF
Statement x;
Example :Write a VB program to find the commission for a salesman according
to the following conditions.
Sales amount
Commission
<= 10000
5 % of sales
> 10000 and <= 30000
10 % of sales
> 30000 and <= 50000
15 % of sales
> 50000
20 % of sales
Private Sub Command1_Click()
Rem Declaration part
Dim sname as String, pname As String
Dim sno as Integer, samt as Integer , bp as Integer
Dim com as Double, net as Double
Rem Input part
sname = InputBox(" Enter Salesman name ")
sno
= InputBox(" Enter Salesman number ")
pname = InputBox(" Enter Product name ")
bp
= InputBox(" Enter Basic pay ")
samt = InputBox(" Enter sales amount ")
Rem Calculation Part
IF samt <= 10000 THEN
com = samt * 5 /100;
ELSEIF samt > 10000 AND samt <= 30000 THEN
com = samt * 10 /100;
ELSEIF samt > 30000 AND samt <= 50000 THEN
com = samt * 15 /100;
ELSE
com = samt * 20 /100;
ENDIF
namt = samt + com;
Rem Output Part
print “Salesman name
:
“ & sname
print “Salesman numbe
:
” & sno
print “Product name
print “Basic pay
print “Sales amount
print “Commission
print “Net amount
End Sub
:
:
:
:
:
” & pname
” & bp
” & samt
” & com
” & namt
d) Select Case statement
• VB has a built – in multiway decision statement known as a Select Case .
• It is an alternative of a complex IF .. ELSE … THEN statement.
• In Select Case structure, the condition is evaluated only once at
the top of the structure.
Syntax :
Select Case expression
case value-1
block 1
case value-2:
block 2
----------Case Else
block n
End Select
Statement x;
Here
expression  is an integer expression or character
value -1 , value -2 ….  are constants of constant expressions
block1, block2… are statement list may contain zero or more statement
Example: Write a VB program to find grade of a student.
Private Sub Command1_Click()
Rem Declaration part
Dim grade As String
Dim mark As Integer, avg As Integer
mark = InputBox(“ Enter the Student mark “)
Rem calculation
avg = mark / 10;
Select Case avg
Case 10
Case 9
Case 8
grade = “ Honours ”
Case 7
Case 6
grade = “ First Division ”
Case 5
grade = “ Second Division ”
Case 4
grade = “ Third Division ”
Case Else
grade = “Fail”
End Select
print “ Grade = ” & grade
End Sub