Computational Thinking + Doing

Selection Logic

Comparing how courses of action (based on certain conditions) are tested and taken—in R, Python, and Julia.

The “fork in the road” symbolizes decision-making or a selection logic, much like “if-then statements” in computer programming. Just as a choice at a fork directs your route, “if” conditions evaluate situations in code and guide actions. Boolean data types are crucial in this analogy, representing the paths: “if” true is one road, “then” false is the other. They mirror binary choices—yes/no or true/false. Similar to how selecting a path decides your journey, booleans help orchestrate how computer programs flow, ensuring statements are executed in a controllable, predictable and accurate manner—in all defined scenarios.

Let’s examine this CS 101 concept that you will use (and reuse) in almost all your code, in most computer programming languages.

Getting Started

If you are interested in reproducing this work, here are the versions of R, Python, and Julia used. Additionally, my coding style here is verbose, in order to trace back where functions/methods and variables are originating from, and make this a learning experience for everyone—including me.

cat(
    R.version$version.string, "-", R.version$nickname,
    "\nOS:", win.version(), R.version$platform,
    "\nCPU:", benchmarkme::get_cpu()$no_of_cores, "x", benchmarkme::get_cpu()$model_name
)
R version 4.2.3 (2023-03-15 ucrt) - Shortstop Beagle 
OS: Windows 10 x64 (build 22631) x86_64-w64-mingw32 
CPU: 24 x 13th Gen Intel(R) Core(TM) i7-13700HX
import sys
print(sys.version)
3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
using InteractiveUtils
InteractiveUtils.versioninfo()
Julia Version 1.9.2
Commit e4ee485e90 (2023-07-05 09:39 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 24 × 13th Gen Intel(R) Core(TM) i7-13700HX
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, goldmont)
  Threads: 1 on 24 virtual cores

Boolean Data Type

# Boolean value in uppercase
r_for_statistics = TRUE
typeof(r_for_statistics)
[1] "logical"
# Boolean value capitalized
python_for_data_science = True
type(python_for_data_science)
<class 'bool'>
# Boolean value in lowercase
julia_for_scientific_computing = false;
typeof(julia_for_scientific_computing)
Bool

Relational (or Comparison) Operators to Compare Two Values

# Equal to
1754 == 1856
[1] FALSE
# Not equal to
1754 != 1856
[1] TRUE
# Greater than
1754 > 1856
[1] FALSE
# Greater than or equal to
1754 > 1856
[1] FALSE
# Less than or equal to
1754 < 1856
[1] TRUE
# Equal to
1754 == 1856
False
# Not equal to
1754 != 1856
True
# Greater than
1754 > 1856
False
# Greater than or equal to
1754 > 1856
False
# Less than or equal to
1754 < 1856
True
# Equal to
1754 == 1856
false
# Not equal to
1754 != 1856
true
# Greater than
1754 > 1856
false
# Greater than or equal to
1754 > 1856
false
# Less than or equal to
1754 < 1856
true

Logical Operations to Combine Condition Statements

FOUNDING_COLUMBIA_UNIVERSITY <- 1754
FOUNDING_UNIVERSITY_MARYLAND <- 1856
FOUNDING_USA <- 1776

# AND operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) && (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
[1] FALSE
# OR operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) || (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
[1] TRUE
# NOT operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) && !(FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
[1] TRUE
FOUNDING_COLUMBIA_UNIVERSITY = 1754
FOUNDING_UNIVERSITY_MARYLAND = 1856
FOUNDING_USA = 1776

# AND operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) and (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
False
# OR operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) or (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
True
# NOT operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) and not(FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
True
const FOUNDING_COLUMBIA_UNIVERSITY = 1754;
const FOUNDING_UNIVERSITY_MARYLAND = 1856;
const FOUNDING_USA = 1776;

# AND operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) && (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
false
# OR operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) || (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
true
# NOT operator
(FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) && !(FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
true

If...else if...else Statement

if (FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA && FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA) {
    print("Both universities are two of nine colonial colleges.")
} else if (FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA) {
    print("Columbia University is one of nine colonial colleges, but the University of Maryland is not.")
} else if (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA) {
    print("The University of Maryland is one of nine colonial colleges, but Columbia University is not.")
} else {
    print("Neither university is one of nine colonial colleges.")
}
[1] "Columbia University is one of nine colonial colleges, but the University of Maryland is not."
if (FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA and FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA):
    print("Both universities are two of nine colonial colleges.")
elif (FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA):
    print("Columbia University is one of nine colonial colleges, but the University of Maryland is not.")
elif (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA):
    print("The University of Maryland is one of nine colonial colleges, but Columbia University is not.")
else:
    print("Neither universities is one of nine colonial colleges.")
Columbia University is one of nine colonial colleges, but the University of Maryland is not.
if (FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA && FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
    print("Both universities are two of nine colonial colleges.")
elseif (FOUNDING_COLUMBIA_UNIVERSITY < FOUNDING_USA)
    print("Columbia University is one of nine colonial colleges, but the University of Maryland is not.")
elseif (FOUNDING_UNIVERSITY_MARYLAND < FOUNDING_USA)
    print("The University of Maryland is one of nine colonial colleges, but Columbia University is not.")
else
    print("Neither universities is one of nine colonial colleges.")
end
Columbia University is one of nine colonial colleges, but the University of Maryland is not.

References

  • Kalicharan, N. (2021). Julia—Bit by Bit: Programming for Beginners. Springer. https://doi.org/10.1007/978-3-030-73936-2
  • Wickham, H. (2019). Advanced R (2nd ed.). CRC. https://doi.org/10.1201/9781351201315
  • Grolemund, G. (2014). Hands-On Programming with R: Write Your Own Functions and Simulations. O’Reilly.
  • Lutz, M. (2013). Learning Python (5th ed.). O’Reilly.
  • Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd ed.). Pearson.
Applied Computing