Announcements Exam 1 Grades Posted on blackboard Average: 83.26 Median: 86 High: 99(4)
Iteration Iteration (Looping): Performing a Series of Statements Multiple Times until Some Condition Is Met. Eliminates the Need for Redundant Coding or Function Calls
The while Loop Syntax: while condition statement; end
while Example MAX = 100; counter = 0; while counter < MAX disp(counter); counter = counter + 1; end
Example while Loop numEmployees = input('Enter Number of Employees: '); if numEmployees > 0 curNum = 0; while curNum < numEmployees disp('Welcome to CorpLand!'); curNum = curNum + 1; end %while end %if
Example while Loop numEmployees = input('Enter Number of Employees: '); if numEmployees > 0 curNum = 0; while curNum < numEmployees disp('Welcome to CorpLand!'); curNum = curNum + 1; end %while end %if
The for Loop Syntax: Same as: for starting assignment:step: terminating value statement end Same as: starting assignment; while variable <= terminating value variable = variable + step;
Example for Loop numEmployees = input('Enter Number of Employees: '); if numEmployees > 0 for curNum = 1:1:numEmployees disp('Welcome to CorpLand!'); end else disp('Hmmmmm!!!');
Example for Loop numEmployees = input('Enter Number of Employees: '); if numEmployees > 0 for curNum = 1:numEmployees %step assumed to be 1 disp('Welcome to CorpLand!'); end else disp('Hmmmmm!!!');
Looping for Input stringEntered = 'go'; while ~strcmp(stringEntered,'stop') stringEntered = input('Enter a string: ','s'); end
Nested Loops for k = 1 : 10 for l = 1 : 10 out1 = sprintf('%d%d ',k,l); disp(out1); end
Quiz 2 Need to Know : Loops : while , for No Terminology on Quiz 2