Why MATLAB showing two different type of value of b?

4 views (last 30 days)
>> 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

Accepted Answer

Image Analyst
Image Analyst on 12 Jul 2015
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!