I wonder about the value of the repeat for.

Asked 2 years ago, Updated 2 years ago, 70 views

void Start()
{
    for (int a = 1; a < 2; a++)
    {
        Debug.Log(a);
    }
}

According to the above repetition, I thought the output value would be 2, but it came out as 1. Is it not applied to the output value because it does not conform to the conditional expression after the value a becomes 2?

unity visual-studio

2022-09-21 17:35

1 Answers

You're right.

The for repeat statement is a for (initial expression; conditional expression; increment/decrease expression) configuration.

The initialization formula runs only the first time. Evaluate the conditional expression before every loop starts, and execute the in-block code only when the conditional expression is true. The increment/decrease expression runs at the end of each loop.

Therefore, in the second loop, a becomes 2 and a < 2 becomes false to escape the repeat statement.


2022-09-21 17:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.