VBA user tried using Python / R: conditional branching

Introduction

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

Conditional branch

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.

Summary

List

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,...,
...)

Whole program

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

reference

Recommended Posts

VBA user tried using Python / R: conditional branching
VBA user tried using Python / R: Matrix
VBA user tried using Python / R: Iterative processing
VBA user tried using Python / R: string manipulation
VBA user tried using Python / R: basic grammar
VBA user tried using Python / R: String manipulation (continued)
VBA users tried using Python / R: basic arithmetic operations
VBA users tried using Python / R: logical operations and comparison operations
[Python] I tried using OpenPose
I tried using Thonny (Python / IDE)
[Python] I tried using YOLO v3
Conditional branching of Python learned by chemoinformatics
I tried using Bayesian Optimization in Python
I tried using UnityCloudBuild API from Python
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV
I tried using mecab with python2.7, ruby2.3, php7
I tried reading a CSV file using Python
I tried using the Datetime module by Python
Start using Python
Scraping using Python
[Python] I immediately tried using Pylance's VS Code extension.
I tried using TradeWave (BitCoin system trading in Python)
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
Paiza Python Primer 2: Learn Conditional Branching and Comparison Operators
[Python] Chapter 05-01 Control syntax (comparison operator and conditional branching)
I tried using Python (3) instead of a scientific calculator
I tried to access Google Spread Sheets using Python