if and select statement
TRANSCRIPT
Conditional Statement
Prepared By :- Rahul Sharma
Enrollment No. : 07521102013
Subject :- VB .NET
Faculty :- Mr. Neeraj Mishra
Selection or Condition ConstructThe selection or decision construct means theexecution of statement(s) depending upon thecondition-test.
If a condition evaluates to True, a course-of-action (aset of statements) is followed otherwise anothercourse-of-action is followed.
This construct is also called decision construct as ithelps in decision making.
Selection or Condition ConstructsVB provides two types of selection construct :
1. If statement
2. Select Case statement
The If Statement : If statement of VB comes in various forms & are given below:
1) If..Then Statement
2) If..Then..Else Statement
3) If..Then..ElseIf Statement
4) Nested Ifs
If..Then StatementDefinition : An If..Then statement tests a particularcondition; if the condition evaluates to true, a course-of-action is followed otherwise it is ignored.
Syntax : If (boolean expression) Then
statementsEnd If
Example :If txtAge.Text>=18 Then
MsgBox(“You are eligible to vote”)End if
If..Then..Else StatementIf..Then..Else statement provides an alternate choice to the useri.e. if the condition is true then a set of statements are executedotherwise another set of statements are executed.
Syntax :If (boolean Expression) Then
VB Statement(s)Else
VB Statement(s)End If
Example :If (txtAge.Text>=18) Then
MsgBox(“You are eligible to vote”)Else
MsgBox(“Sorry, You are not eligible to vote”)End If
If..Then..ElseIf Statement
If..Then..ElseIf statement is used to test a number of mutuallyexclusive cases and only executes one set of statements for the casethat is true first.
Example:If (Age<=4) Then
MsgBox(“Your rate is free.”)ElseIf (Age<=12) Then
MsgBox(“You qualify for the children’s rate.”)ElseIf (Age<65) Then
MsgBox(“You must pay full rate”)Else
MsgBox(“You qualify for the seniors’ rate.”)End If
Syntax : If (Boolean Expression) ThenStatement(s)
ElseIf (Boolean Expression 2) ThenStatement(s)
ElseIf (Boolean Expression 3) ThenStatement(s)
:Else
Statement(s)End If
Nested IfsA nested If is an if that has another If in its if ’s body or in itselse’s body.The nested if can have one of the following 3 forms :-
1. If (expression 1) Then If (expression 2 ) Then
Statement 1Else
Statement 2End If
Elsebody-of-else
End If
2. If (expression 1) Then
body-of-if
Else
:
If (expression 2) Then
Statement-1
Else
Statement-2
End If
3. If (expression 1) Then:
If (expression 2) ThenStatement-1
ElseStatement-2
End IfElse
If (expression 3) ThenStatement-3
ElseStatement-4
: End If
End If
Example of Nested If:
If Num>0 Then
Msgbox(“It is a positive number”)
Else
If Num<0 Then
Msgbox(“It is a negative number”)
Else
Msgbox(“The number is equal to zero”)
End If
End If
Select-Case StatementSelect-Case is a multiple branching statement and isused to executed a set of statements depending uponthe value of the expression.
It is better to use Select-Case statement in comparisonto If..Then..ElseIf Statement when the number ofchecks are more.
There are 3 different forms of using Select-Casestatements and are given below :
Different forms of Select-Case
Select Case Expression
Case Value
’visual basic statements
Case Value
’visual basic statements
Case Else
’visual basic statements
End Select
1. Select Case : Simplest Form [Exact match]
Example :
Select Case byMonthCase 1,3,5,7,8,10,12
number_of_days=31Case 2
number_of_days=28Case Else
number_of_days=30End Select
Select Case Expression
Case Is relation
’visual basic statements
Case Is relation
’visual basic statements
Case Else
’visual basic statements
End Select
Example:
Select Case marksCase Is < 50
Result = “Fail”Case Is < 60
Result = “Grade B”Case Is < 75
Result = “Grade A”Case Else
Result = “Grade A+”End Select
2.Select Case : Second Form [Relational Test]
Select Case Expression
Case exp1 To exp2:
’visual basic statements
Case exp1 To exp2:
’visual basic statements
Case Else:
’visual basic statements
End Select
Example :
Select Case AgeCase 2 to 4 : Msgbox(“PreNursery”)Case 4 to 6 : Msgbox(“Kindergarden”)Case 6 to 10 : Msgbox(“Primary”)Case Else : Msgbox(“Others”)
End Select
3.Select Case : Third Format [Range Check]