S
S
SteveJ422021-11-26 07:39:37
Swift
SteveJ42, 2021-11-26 07:39:37

Can't understand this code?

let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
    if score > 50 {
        teamScore += 3
    } else {
        teamScore += 1
    }
}
print(teamScore)

I can’t understand where the initial values ​​\u200b\u200bare taken from the score variable inside the loop at the beginning

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2021-11-26
@SteveJ42

The initial value is the first element of the individualScores array.
And in this code, the order of iteration is not even important, so, one might say, there is no initial value at all. That is, score is just one of the array elements in each of the iterations of the loop.
In general, the question "I can not understand this code?" the formal answer is "Yes, you can't."
If you want to understand the code, then the question must be asked correctly: not about whether you understand or not, not about the very fact of understanding, but about understanding the code:
"How to understand this code?"
It is also desirable to make the question more specific, so that it is clearer from the title what the question is about. It's hard, but you have to learn. For example, like this:
"Where does the initial value in the for in loop come from?"
And lastly: writing carelessly and without commas is disrespectful to those who answer. It can be seen that you did not even reread your own question, the words are repeated twice and do not add up to a sentence, even if you put down commas. Therefore, there will be fewer people willing to answer. I'm only answering because the question is too simple.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question