Skip to main content

11.13) while...end Statement: Examples (cont'd)


Count the number of lines in the file "mean.m"

[matlab]
>> % Open mean.m
>> fid=fopen('mean.m', 'r');
>> count=0;
>> while (feof(fid)~=1) % feof is the end of file test
>> line=fgetl(fid); % fgetl reads a new line from the file
>> if (isempty(line) || (strncmp(line, '%', 1) || (ischar(line)~=1)
>> continue
>> end
>> count=count+1;
>> end
>> display([num2str(count), 'lines of code'])
[/matlab]

The continue statement passes control to the next iteration of the while loop. It skips the remainder of the current iteration, so it doesn't do "count=count+1".