A foreach diagram must get the next element before using its value. An inner scoring decision must not accidentally terminate the outer traversal.
This excerpt runs inside a method. It totals positive round scores and counts scoring rounds. The input contains no negative scores.
If the underlying C# feature is unfamiliar, use the lesson below while reading this example.
int[] roundGoals = { 2, 0, 3 };
int totalGoals = 0;
int scoringRounds = 0;
foreach (int goals in roundGoals)
{
if (goals > 0)
{
totalGoals = totalGoals + goals;
scoringRounds = scoringRounds + 1;
}
}Initialize the score sequence and both counters
Begin visiting the sequence in order
If another element exists, obtain its value as goals
If that score is positive, add it to totalGoals and increase scoringRounds
Continue to the next element whether or not the score was positive
Finish only when no element remainsUse these words to follow one execution before drawing it. Keep the same order and the same alternatives as the source; simplify the wording without adding work that the code does not perform.
roundGoals = [2, 0, 3], totalGoals = 0, scoringRounds = 0, and Begin traversal.Another element?. Its true route reaches Get next value as goals and then the inner diamond goals > 0?.true route draw actions totalGoals = totalGoals + goals and scoringRounds = scoringRounds + 1. Its false route bypasses both.false route to the activity final.Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.
The score 2 makes totalGoals 2 and scoringRounds 1. Zero changes neither. The final 3 makes them 5 and 2 before traversal ends. For an empty input array, the first outer check exits immediately.