I am a VBA user who started studying machine learning. As a memorandum, I would like to summarize the Python / R grammar while comparing it with VBA.
table of contents
-Conditional branch -Conditional branch -Summary -List -Whole program
About conditional branching (mainly If statement).
Python
Python3
x = 0
if x == 0:
print(0)
# 0
if x == 0:
print(0)
else:
print(1)
# 0
if x == 0:
print(0)
elif x == 1:
print(1)
elif x == 2:
print(2)
else:
print(3)
# 0
if x in (0,1,2):
print(x)
else:
print(3)
# 0
There seems to be no switch statement or case statement in Python.
In Python, a group (block) of If statements is enclosed in {``}
as in R below, or starts with If
and closes with End If
as in VBA. It is expressed by indentation (indentation) (not limited to If statements, but also to other control syntax (repeated For statements)).
R
R
x <- 0
if (x == 0) {
print(0)
}
# 0
if (x == 0) {
print(0)
} else {
print(1)
}
# 0
if (x == 0) {
print(0)
} else if (x == 1) {
print(1)
} else if (x == 2) {
print(2)
} else {
print(3)
}
# 0
if (x %in% c(0,1,2)) {
print(x)
} else {
print(3)
}
R has a switch
function, but it seems difficult to use (reference: R-Tips).
VBA
VBA
x = 0
If x = 0 Then
Debug.Print 0
End If
' 0
If x = 0 Then
Debug.Print 0
Else
Debug.Print 1
End If
' 0
If x = 0 Then
Debug.Print 0
ElseIf x = 1 Then
Debug.Print 1
ElseIf x = 2 Then
Debug.Print 2
Else
Debug.Print 3
End If
' 0
Select Case x
Case 0
Debug.Print 0
Case 1
Debug.Print 1
Case 2
Debug.Print 2
Case Else
Debug.Print 3
End Select
' 0
Select Case x
Case 0, 1, 2
Debug.Print x
Case Else
Debug.Print 3
End Select
' 0
Select Case x
Case 0 To 2
Debug.Print x
Case Else
Debug.Print 3
End Select
'0
Select Case x
Case Is <= 2
Debug.Print x
Case Else
Debug.Print 3
End Select
' 0
Debug.Print Switch(x = 0, 0)
' 0
Debug.Print Switch(x = 0, 0, 1, 1)
' 0
Debug.Print Switch(x = 0, 0, 1, 1, 2, 2)
' 0
VBA has a Select Case statement. There is also a Switch function.
List the operators used in each language. For comparison, the calculation in EXCEL is also shown.
*** Conditional branch ***
Python | R | VBA | EXCEL | |
---|---|---|---|---|
If statement | if P: ... elif Q: ... elif R: ... else: ... |
if (P) { ... } else if (Q) { ... } else if (R) { ... } else { ... } |
If P Then ... ElseIf Q Then ... ElseIf R Then ... Else ... End If |
=IF(P,..., IF(Q,..., IF(R,..., ...))) |
Switch statement | Select Case X Case p ... Case q ... Case r ... Case Else ... End Select |
=SWITCH(X, p,..., q,..., r,... ,...) |
The whole program used for reference is shown.
Python
Python3
#Conditional branch
x = 0
if x == 0:
print(0)
# 0
if x == 0:
print(0)
else:
print(1)
# 0
if x == 0:
print(0)
elif x == 1:
print(1)
elif x == 2:
print(2)
else:
print(3)
# 0
if x in (0,1,2):
print(x)
else:
print(3)
# 0
R
R
#Conditional branch (If)
x <- 0
if (x == 0) {
print(0)
}
# 0
if (x == 0) {
print(0)
} else {
print(1)
}
# 0
if (x == 0) {
print(0)
} else if (x == 1) {
print(1)
} else if (x == 2) {
print(2)
} else {
print(3)
}
# 0
if (x %in% c(0,1,2)) {
print(x)
} else {
print(3)
}
# 0
VBA
VBA
Sub test_conditional_branch()
Dim x As Integer
'Conditional branch
x = 0
If x = 0 Then
Debug.Print 0
End If
' 0
If x = 0 Then
Debug.Print 0
Else
Debug.Print 1
End If
' 0
If x = 0 Then
Debug.Print 0
ElseIf x = 1 Then
Debug.Print 1
ElseIf x = 2 Then
Debug.Print 2
Else
Debug.Print 3
End If
' 0
Select Case x
Case 0
Debug.Print 0
Case 1
Debug.Print 1
Case 2
Debug.Print 2
Case Else
Debug.Print 3
End Select
' 0
Select Case x
Case 0, 1, 2
Debug.Print x
Case Else
Debug.Print 3
End Select
' 0
Select Case x
Case 0 To 2
Debug.Print x
Case Else
Debug.Print 3
End Select
'0
Select Case x
Case Is <= 2
Debug.Print x
Case Else
Debug.Print 3
End Select
' 0
Debug.Print Switch(x = 0, 0)
' 0
Debug.Print Switch(x = 0, 0, 1, 1)
' 0
Debug.Print Switch(x = 0, 0, 1, 1, 2, 2)
' 0
End Sub
Recommended Posts