While Loops [R,Python]

In programming, loops allow us to perform repetitive actions without having to write repetitive code. Here are a few examples:

[R]

# Let's declare and initialize a variable temp
temp <- 75
# Let's write a loop
while (temp > 65) {
    print(paste("The temperature is:",temp))
    if (temp > 80) {
        print("Too hot!")
        temp <- temp - 20
        print(paste("Temperature has been reset to:",temp))
    } else {
        print("Still too warm!")
        temp <- temp - 10
        print(paste("Temperature has been reset to:",temp)) 
    }
}

[output]
[1] "The temperature is: 75"
[1] "Still too warm!"
[1] "Temperature has been reset to: 65"
[Python]

# Let's declare and initialize a variable temp
temp = 75

#Now, let's create a loop
while (temp > 65):
  print(f'The temperature is: {temp}')
  if (temp > 80):
    print("Too hot!")
    temp -= 20
    print(f'Temperature has been reset to {temp}')
  else:
    print("Still too warm!")
    temp -= 10
    print(f'Temperature has been reset to {temp}')

[output]
The temperature is: 75
Still too warm!
Temperature has been reset to 65

Leave a comment