% Comments (non-executed instuctions) always begin with % the PERCENT SYMBOL. % This is a function statement. % When we type: % locker(10) % in the Command Window, the script % locker.m will be executed with 10 lockers! function y = locker(n) % The variable a is an n-by-1 column vector of zeros. % 0 = closed and 1 = open! a = zeros(n,1); % This identifies which student is playing with the % lockers. We begin with student #1 and then eventually % move up to student #n. for i = 1:n % This loop is nested inside the first one. % This starts with j = i, and then % STEPS by i units. % For example: % for j = 3:3:20 % will produce j = 3, then j = 6, then j = 9, % until we reach 20. Notice that j = 18 is the % last number generated. for j = i:i:n % If the jth locker is closed, then open it. % Else, if the jth locker is open, then close it. % Note that the logical test for "equals" is "==". if a(j,1)==0; a(j,1)=1; else a(j,1)=0; % Every "if" statement needs an "end". end % Every "for" loop needs an "end". end end for i = 1:n % This is the print statement. % We will print the locker number i, and its status. fprintf(' %5.0f %2.0f\n',i,a(i,1)); end