Computational Thinking + Doing

Repetition Logic

Executing a set of statements—in R, Python, and Julia—for a specified number of times or while a condition is true.

“Loops” in computer programming mirror everyday repetitive tasks. Just as a washing machine cycles clothes until clean, “for” loops iterate data, like scrubbing each element. Like reading a book’s pages sequentially, “while” loops repeatedly execute until a condition, like finishing chapters. A chef tasting a dish and adjusting flavors echoes a “do-while” loop—it ensures the recipe’s quality. These loops streamline tasks, repeating actions systematically until completion or satisfaction, reflecting the essence of both programming logic and real-world routines.

Let’s dive into 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 (as well as the respective packages for each). 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

2002 NCAA Men’s Basketball Final Top 25 Ranking

Almost a decade later (from the original date of this post), I attended graduate business school at the University of Maryland, where its men’s basketball team won the 2002 NCAA national championship. (Go Terps!) I’ll use the 2002 final Top 25 rankings to demonstrate the various forms of repetition logic.

FINAL_NCAA_MBB_2002_R <- c(
    "Maryland",
    "Kansas",
    "Indiana",
    "Oklahoma",
    "Duke",
    "Oregon",
    "UConn",
    "Cincinnati",
    "Pittsburgh",
    "Arizona",
    "Illinois",
    "Kent State",
    "Kentucky",
    "Alabama",
    "Missouri",
    "Gonzaga",
    "Ohio State",
    "Marquette",
    "Texas",
    "UCLA",
    "Mississippi State",
    "Southern Illinois",
    "Florida",
    "Xavier",
    "NC State"
)
typeof(FINAL_NCAA_MBB_2002_R)
[1] "character"
FINAL_NCAA_MBB_2002_R[1]
[1] "Maryland"
length(FINAL_NCAA_MBB_2002_R)
[1] 25
FINAL_NCAA_MBB_2002_PY = [
    "Maryland",
    "Kansas",
    "Indiana",
    "Oklahoma",
    "Duke",
    "Oregon",
    "UConn",
    "Cincinnati",
    "Pittsburgh",
    "Arizona",
    "Illinois",
    "Kent State",
    "Kentucky",
    "Alabama",
    "Missouri",
    "Gonzaga",
    "Ohio State",
    "Marquette",
    "Texas",
    "UCLA",
    "Mississippi State",
    "Southern Illinois",
    "Florida",
    "Xavier",
    "NC State"
]
type(FINAL_NCAA_MBB_2002_PY)
<class 'list'>
FINAL_NCAA_MBB_2002_PY[0] # Zero-based array indexing
'Maryland'
len(FINAL_NCAA_MBB_2002_PY)
25
const FINAL_NCAA_MBB_2002_JL = [
    "Maryland",
    "Kansas",
    "Indiana",
    "Oklahoma",
    "Duke",
    "Oregon",
    "UConn",
    "Cincinnati",
    "Pittsburgh",
    "Arizona",
    "Illinois",
    "Kent State",
    "Kentucky",
    "Alabama",
    "Missouri",
    "Gonzaga",
    "Ohio State",
    "Marquette",
    "Texas",
    "UCLA",
    "Mississippi State",
    "Southern Illinois",
    "Florida",
    "Xavier",
    "NC State"
];
typeof(FINAL_NCAA_MBB_2002_JL)
Vector{String} (alias for Array{String, 1})
FINAL_NCAA_MBB_2002_JL[1]
"Maryland"
length(FINAL_NCAA_MBB_2002_JL)
25

For Loop

for (i in length(FINAL_NCAA_MBB_2002_R):1) {
    if (i == 1) {
        cat("And your 2002 national champions... ")
    }
    cat(paste("#", i, "-", FINAL_NCAA_MBB_2002_R[i]), "\n")
}
# 25 - NC State 
# 24 - Xavier 
# 23 - Florida 
# 22 - Southern Illinois 
# 21 - Mississippi State 
# 20 - UCLA 
# 19 - Texas 
# 18 - Marquette 
# 17 - Ohio State 
# 16 - Gonzaga 
# 15 - Missouri 
# 14 - Alabama 
# 13 - Kentucky 
# 12 - Kent State 
# 11 - Illinois 
# 10 - Arizona 
# 9 - Pittsburgh 
# 8 - Cincinnati 
# 7 - UConn 
# 6 - Oregon 
# 5 - Duke 
# 4 - Oklahoma 
# 3 - Indiana 
# 2 - Kansas 
And your 2002 national champions... # 1 - Maryland 
for i in range(len(FINAL_NCAA_MBB_2002_PY), 0, -1):
    if i == 1:
        print("And your 2002 national champions... ", end = "")
    print("#" + str(i) + " - ", end = "")
    print(FINAL_NCAA_MBB_2002_PY[i - 1])
#25 - NC State
#24 - Xavier
#23 - Florida
#22 - Southern Illinois
#21 - Mississippi State
#20 - UCLA
#19 - Texas
#18 - Marquette
#17 - Ohio State
#16 - Gonzaga
#15 - Missouri
#14 - Alabama
#13 - Kentucky
#12 - Kent State
#11 - Illinois
#10 - Arizona
#9 - Pittsburgh
#8 - Cincinnati
#7 - UConn
#6 - Oregon
#5 - Duke
#4 - Oklahoma
#3 - Indiana
#2 - Kansas
And your 2002 national champions... #1 - Maryland
for i in reverse(1:length(FINAL_NCAA_MBB_2002_JL))
    if i == 1
        print("And your 2002 national champions... ")
    end
    println("#$i - ", FINAL_NCAA_MBB_2002_JL[i])
end
#25 - NC State
#24 - Xavier
#23 - Florida
#22 - Southern Illinois
#21 - Mississippi State
#20 - UCLA
#19 - Texas
#18 - Marquette
#17 - Ohio State
#16 - Gonzaga
#15 - Missouri
#14 - Alabama
#13 - Kentucky
#12 - Kent State
#11 - Illinois
#10 - Arizona
#9 - Pittsburgh
#8 - Cincinnati
#7 - UConn
#6 - Oregon
#5 - Duke
#4 - Oklahoma
#3 - Indiana
#2 - Kansas
And your 2002 national champions... #1 - Maryland

While Loop


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