(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
ElseIf
public class Test
public Shared Sub Main
Dim intScore As Integer
Dim strResult As String
intScore = 78
If intScore < 50 Then
strResult = "Failed."
ElseIf intScore < 75 Then
strResult = "Pass."
ElseIf intScore < 90 Then
strResult = "Very Good."
Else
strResult = "Excellent."
End If
Console.WriteLine(strResult)
End Sub
End class
Very Good.
Else If (Not ELSEIF)
Imports System
Public Class Test
Shared Sub Main()
Dim dtCurrent As System.DateTime
Dim iHour As Integer
dtCurrent = dtCurrent.Now()
iHour = dtCurrent.Hour
If (iHour < 12) Then
Console.Writeline("Good Morning!")
Else If (iHour >= 12) And (iHour < 18) Then
Console.WriteLine("Good Afternoon!")
Else
Console.WriteLine("Good Evening!")
End If
End Sub
End Class
Very Good.
If Short Circuiting with ANDALSO
Public Class ShortCircuiting
Shared Sub Main()
If Test("Left") ANDALSO Test("Right") Then
"do something
End If
End Sub
Shared Function Test(sInput As String) As Boolean
System.Console.WriteLine(sInput)
Test = FALSE
End Function
End Class
Left
If statement Short Circuiting
Public Class ShortCircuiting
Shared Sub Main()
If Test("Left") AND Test("Right") Then
"do something
End If
End Sub
Shared Function Test(sInput As String) As Boolean
System.Console.WriteLine(sInput)
Test = FALSE
End Function
End Class
Left
Right
If with And
Imports System
Public Class Test
Shared Sub Main()
Dim dtCurrent As System.DateTime
Dim iHour As Integer
dtCurrent = dtCurrent.Now()
iHour = dtCurrent.Hour
If (iHour < 12) Then
Console.Writeline("Good Morning!")
ElseIf (iHour >= 12) And (iHour < 18) Then
Console.WriteLine("Good Afternoon!")
Else
Console.WriteLine("Good Evening!")
End If
End Sub
End Class
Good Afternoon!
Nested if statement
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim temp As Integer = 32
If temp <= 32 Then
If temp = 32 Then
Console.WriteLine("temp = 32")
Else
Console.WriteLine("Temp: {0}", temp)
End If
End If
End Sub
End Module
temp = 32
Single line vs Multi-line If statement
public class Test
public Shared Sub Main
Dim intScore As Integer
intScore = 23
"Single line If statement.
If intScore < 50 Then Console.WriteLine("Failed.") : Console.WriteLine("Please try again.")
"Multi-line If statement.
If intScore < 50 Then
Console.WriteLine("Failed.")
Console.WriteLine("Please try again.")
End If
End Sub
End class
Failed.
Please try again.
Failed.
Please try again.
Use ElseIf
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim temp As Integer = -32
If temp > 32 Then
ElseIf temp = 32 Then
Console.WriteLine("= 32")
ElseIf temp > 0 Then
ElseIf temp = 0 Then
Console.WriteLine(" = 0")
Else
Console.WriteLine("Else")
End If
End Sub "Main
End Module
Else
Use If statement in a For statement
public class Test
public Shared Sub Main
Dim intCounter As Integer
Dim strString As String = "some silly string"
For intCounter = 0 To strString.Length - 1
If strString(intCounter) = "s" Then
strString = strString.Remove(intCounter, 1)
strString = strString.Insert(intCounter, "S")
End If
Next intCounter
Console.WriteLine(strString)
End Sub
End class
Some Silly String
Use if statement to compare Integer
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim valueOne As Integer = 10
Dim valueTwo As Integer = 20
Dim valueThree As Integer = 30
If valueOne > valueTwo Then
Console.WriteLine("ValueOne: {0} larger than ValueTwo: {1}", valueOne, valueTwo)
End If
If valueThree > valueTwo Then
Console.WriteLine("ValueThree: {0} larger than ValueTwo: {1}",valueThree, valueTwo)
End If
If valueTwo > 15 Then
Console.WriteLine("Yes it is")
End If
End Sub "Main
End Module
ValueThree: 30 larger than ValueTwo: 20
Yes it is