While Loop Question (beginner)

Hello,

I'm new to R and still trying to figure out things. One of my homework assignments is to create a While Loop. Here are my instructions:

Create a new rscript file called (exactly) Challenge32C.R. Make sure you save it in the /workspace directory. Write a While Loop statement that prints all numbers to the console from within the while loop from -1 to 1 in .25 increments.

Here is the code I wrote:
#Control Flow -- WHILE LOOP
die1<- -1.25 #set control variable
while (die1<-1.25) { #check condition of control variable
vd1d2<-c((die1+.25),(die1+.5),(die1+.75),(die1+1),(die1+1.25),(die1+1.5),(die1+1.75),(die1+2)) #create vector
print(vd1d2) #print vector as you process each die1
die1<-die1+.25 #increment control variable
}

I know I'm on the right track, but something is off and I have no idea what. I've tried making minor changes but still don't seem closer to the answer. I keep getting the numbers "[1] 1.50 1.75 2.00 2.25 2.50 2.75 3.00 3.25" repeating over and over and over.

Any insight you have, I'd really appreciate. Thank you!

Look carefully at the condition you have written for the while(). You have

 while( die1 <-  1.25)

Does <- do what you want?

Can you explain what you mean? We have an example I'm using to make my own loop. The first line of the While Loop in the example is
die1<-1 #set control variable
so I'm trying to make the same thing here? I'm not sure how I would do it differently.

Now I understand the question better, sorry, my mistake.
I've tried changing around the while( die1 <- 1.25) line but I just keep getting answers that are close but not it. I tried 0 and while this was the closest, it didn't give me the correct answer either, so I'm still fairly confused.

I'm still trying to work on it, and I've gotten a little further, but I'm still confused.
When I just print the vector, I get the result I want. I don't know why I'm getting the table of numbers?
Here's what I've done

R Script:
#Control Flow -- WHILE LOOP
die1<- -1 #set control variable
while (die1<.25) { #check condition of control variable
vd1d2<-c((die1-1), (die1-.75), (die1-.5), (die1-.25), (die1+0), (die1+.25),(die1+.5),(die1+.75),(die1+1)) #create vector
die1<-die1+.25 #increment control variable
print(vd1d2) #print vector as you process each die1
}

Console output:

#Control Flow -- WHILE LOOP
die1<- -1 #set control variable
while (die1<.25) { #check condition of control variable

  • vd1d2<-c((die1-1), (die1-.75), (die1-.5), (die1-.25), (die1+0), (die1+.25),(die1+.5),(die1+.75),(die1+1)) #create vector
  • die1<-die1+.25 #increment control variable
  • print(vd1d2) #print vector as you process each die1
  • }
    [1] -2.00 -1.75 -1.50 -1.25 -1.00 -0.75 -0.50 -0.25 0.00
    [1] -1.75 -1.50 -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25
    [1] -1.50 -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50
    [1] -1.25 -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75
    [1] -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00

print(vd1d2) #print vector as you process each die1
[1] -1.00 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 1.00

Your original code was quite close to the solution. One error, which you have fixed in the latest code is to use < instead of <- inside of the while(). The second problem, in my opinion, is that you are printing whole vector vd1d2. I think you should print just die1 in each increment of the loop. The following is not quite correct but if you try it, I think you can finish it up on your own.

die1<- -1.25 #set control variable
while (die1 < 1.25) { #check condition of control variable
  vd1d2<-c((die1+.25),(die1+.5),(die1+.75),(die1+1),(die1+1.25),(die1+1.5),(die1+1.75),(die1+2)) #create vector
  #print(vd1d2) #print vector as you process each die1
  print(die1)
  die1 <- die1+.25 #increment control variable
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.