<jupyter_start><jupyter_text>A loop is a statement that keeps running until a condition is satisfied. The syntax for a while loop is the following:<jupyter_code>while (condition) {
     Exp	
}<jupyter_output><empty_output><jupyter_text>![](https://lh3.googleusercontent.com/-CVhNfUjZzKk/XZGl4DuCrKI/AAAAAAAAiOk/NSh4xfJ5hq0oU_4zmAwYZNu9IURZGwAngCK8BGAsYHg/s0/2019-09-29.png) **Note:** Remember to write a closing condition at some point otherwise the loop will go on indefinitely.**Example 1:**Let's go through a very simple example to understand the concept of while loop. You will create a loop and after each run add 1 to the stored variable. You need to close the loop, therefore we explicitely tells R to stop looping when the variable reached 10.**Note:** If you want to see current loop value, you need to wrap the variable inside the function print().<jupyter_code>#Create a variable with value 1
begin <- 1

#Create the loop
while (begin <= 10){

#See which we are  
cat('This is loop number',begin)

#add 1 to the variable begin after each loop
begin <- begin+1
print(begin)
}<jupyter_output>This is loop number 1[1] 2
This is loop number 2[1] 3
This is loop number 3[1] 4
This is loop number 4[1] 5
This is loop number 5[1] 6
This is loop number 6[1] 7
This is loop number 7[1] 8
This is loop number 8[1] 9
This is loop number 9[1] 10
This is loop number 10[1] 11<jupyter_text>Example 2:You bought a stock at price of 50 dollars. If the price goes below 45, we want to short it. Otherwise, we keep it in our portfolio. The price can fluctuate between -10 to +10 around 50 after each loop. You can write the code as follow:<jupyter_code>set.seed(123)
# Set variable stock and price
stock <- 50
price <- 50

# Loop variable counts the number of loops 
loop <- 1

# Set the while statement
while (price > 45){

# Create a random price between 40 and 60
price <- stock + sample(-10:10, 1)

# Count the number of loop
loop = loop +1 

# Print the number of loop
print(loop)
}
cat('it took',loop,'loop before we short the price. The lowest price is',price)<jupyter_output><empty_output><jupyter_text>Pracites<jupyter_code>i <- 1
while (i < 6){
    print(i)
    i=i+1
}
# Find the Sum of Natural Numbers 

num = as.integer(readline(prompt = "Enter the Number"))

if(num <0){
    print("Enter the Postive Number:")
}else{
    sum = 0
    while (num>0){
        sum = sum + num
        num = num-1
    }
    print(paste("The Sum is",sum))

}

# Fibonacci Sequence 
nterms = as.integer(readline(prompt = "How Many Terms"))

n1 = 0
n2 = 1

count = 2

if(nterms<=0){
    print("Please Enter a Postive Number")
}else{
    if(nterms == 1){
        print("Fibonacci Number:")
        print(n1)
        
    }
    else{
        print("Fibonacci Sequence")
        print(n1)
        print(n2)
        while(count < nterms){
            nth = n1 + n2
            print(nth)
            # Update vlaues
            n1 = n2
            n2 = nth
            count= count + 1            
        }
    }
}

# Initialize Sum
sum = as.integer(readline(prompt = "Enter Number:")) 

# Find the sum of the cub of each digits
temp = num
while (temp > 0){
    digit = temp %% 10
    sum = sum +(digit^3)
    temp = floor(temp/10)
}
if(num == sum){
    print(paste(num,'is an armstrong number'))
}else{
    print(paste(num,'is not an armstrong number'))
}<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup