Initialization of loop index when loop is not entered

2 views (last 30 days)
I notice that loop indices are initialized to empty double even when the loop is not entered. Is this undocumented behavior or something that is a stable feature that we can depend on in our code? (Matlab 2016a)
>> clear
>> for k=1:0;end
>> whos
Name Size Bytes Class Attributes
k 0x0 0 double

Answers (1)

Star Strider
Star Strider on 29 Apr 2017
The colon operator by defauklt increments by 1 unless you tell it to increment by a different positive or negative step.
In:
k = 1:0
the loop is ‘satisfied’ at the outset, so no increment takes place and ‘k’ is empty. I can’t find that in a brief scan of the documentation on colon,: (link). It could be documented elsewhere.

Categories

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

Tags

Products

Community Treasure Hunt

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

Start Hunting!