MATLAB: MATLAB showing two different type of value of b

for loop

>> a
a =
1 4 7 9 22
>> for(b = a); b; end
>> b
b =
22
>> for (b = a)
b
end
b =
1
b =
4
b =
7
b =
9
b =
22

Best Answer

In the first case, you have a semicolon at the end of the line which means it does not echo the current value of b to the command line. After the loop you put b without a semicolon, so it will give the current value of b, which is 22.
In the second case you left off the semicolon inside the loop so it will echo/print b to the command window on every iteration. That's why you see all of them instead of just the last value like you did in the first case.
In both cases, b took on exactly the same values.