Variable declaration reset: Difference between revisions

no edit summary
m (→‎{{header|Python}}: more explanation)
No edit summary
Line 576:
 
End Module</lang>
{{out}}
<pre>
2
5
</pre>
 
=={{header|Vlang}}==
Note firstly that unassigned variables are impossible in Vlang. If a variable is created it must have an explicit value, then it is assigned the default value for its type which in the case of numbers is zero. Fortunately, this doesn't clash with values in the slice in the following program.
<lang vlang>fn main() {
s := [1, 2, 2, 3, 4, 4, 5]
// There is no output as 'prev' is created anew each time
// around the loop and set implicitly to zero.
for i := 0; i < s.len; i++ {
curr := s[i]
mut prev := 0
if i > 0 && curr == prev {
println(i)
}
prev = curr
}
// Now 'prev' is created only once and reassigned
// each time around the loop producing the desired output.
mut prev := 0
for i := 0; i < s.len; i++ {
curr := s[i]
if i > 0 && curr == prev {
println(i)
}
prev = curr
}
}</lang>
 
{{out}}
<pre>
338

edits