I need help with applying this for loop request


#1

Hello guys i need help with understanding this question and testing it out.
How can I Write a for loop that will – for 5 times -- read a TestScore value from the keyboard and accumulate the 5 scores into a variable named TotalScore ?


#2

A standard for loop goes along the lines of

test_scores = c(1,5,3,0,5)
total_score = 0
for( score in test_scores ){
    total_score = total_score + score
}
print(total_score)

Usually one would not read from the keyboard, as it non-reproducible - Why are you interested in doing so?


#4

Thank you very much. I am trying to understand the basic applications in Rstudio. My brain works best if I see examples and answers then interpreting it and analysing the answers.


#5

If you really want to do a sum of a numeric vector, then this is the way forth:

test_scores = c(1,5,3,0,5)
total_score = sum(test_scores)

Btw. compared to a car, the R is the engine and RStudio everything around it, i.e. the rest of the car (aka an IDE) :+1: