Examples of while loop in MATLAB
All Contributions
Count the number of lines of code in the file (magic.m) . Skip blank lines and comments using a continue statement. continue skips the remaining instructions in the while loop and begins the next iteration.
fid = fopen('magic.m','r');
count = 0;
while ~feof(fid)
line = fgetl(fid);
if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
continue
end
count = count + 1;
end
count
total contributions (3)
Sum a sequence of random numbers until the next random number is greater than an upper limit. Then, exit the loop using a break statement.