Loops permit us to execute a sequence
They differ in how the repetition is
Compute the average of n values entered by the
user. The number of values (n) wil be specified by
n = input('Enter the number of values: ');counter = 0; total = 0;while (counter < n )
total = total + x; counter = counter + 1;endif (n > 0) avg = total / counter; disp(['The average is ' num2str(avg)]);end
Compute the average of values entered by the user.
A negative value indicates the end of the input.
counter = 0;total = 0;x = input('Enter the first value: ');while (x >= 0 ) counter = counter + 1; total = total + x; x = input('Enter the next value: ');endif (counter > 0) avg = total / counter; disp(['The average is ' num2str(avg)]);end
Example: “Change case” Change the case of letters in a sentence.
k = 1;while (k <= length(s)) if (s(k) >= 'A') & (s(k) <= 'Z') new_s(k) = char('a' + (s(k) - 'A')); elseif (s(k) >= 'a') & (s(k) <= 'z') new_s(k) = char('A' + (s(k) - 'a')); else new_s(k) = s(k); end k = k + 1;endnew_s
“for” Loop Statements are executed a specified number
No of repetitions is known before the loop starts
Expression is usual y a vector in shortcut
Using “for” loop, compute the average of n values
entered by the user. The number of values (n) wil be
n = input('Enter the number of values: ');total = 0;for counter = 1 : n
total = total + x;endif (counter > 0) avg = total / counter; disp(['The average is ' num2str(avg)]);end
Example: “Factorial calculation” Calculate the factorial (N!) of an integer N;
the factorial of negative integers is not
N = input('Enter a non-negative integer: ');
if N < 0 disp(['It is a negative integer']);else result = 1; for i = 1 : N, result = result *i; end disp([num2str(N) '! = ' num2str(result)]);
Write a program that finds the first
A positive integer n is a perfect number if
the sum of its positive divisors excluding n
e.g., 28 is a perfect number; 1+2+4+7+14=28
Example: “Perfect numbers”counter = 1;no = 1;
while counter <= 3 total = 0; for ii = 1 : no/2 if mod(no,ii) == 0 total = total + i ; if no == total
perfect_nos(counter) = no; counter = counter + 1;
end no = no + 1; end fprintf('The first three perfect numbers are ') for counter = 1 : 3
fprintf('%d ',perfect_nos(counter)); end
Compute the multiplication of two matrices.
If A is an m-by-n matrix and B is an n-by-p matrix,
their product is an m-by-p matrix C which is given
The matrix multiplication is defined between two
matrices only if the number of columns of the 1st
matrix is the same as the number of rows of the
Example: “Matrix multiplication”[row_A,column_A] = size(A);[row_B,column_B] = size(B);
if column_A ~= row_B disp('Matrix dimensions must agree'); else for ii = 1 : row_A for jj = 1 : column_B C(ii,jj) = 0; for k = 1 : column_A C(ii,jj) = C(ii,jj) + A(ii,k) * B(k,jj); end end end end
Use indentation to improve the readability of
Never modify the value of a loop index inside
Al ocate al arrays used in a loop before executing
If it is possible to implement a calculation either
with a loop or using vectors, always use vectors
Use built-in MATLAB functions as much as possible
C4(i ,jj) = C4(ii,jj) + A(ii,k) * B(k,jj);
C2(i ,jj) = C2(i ,jj) + A(i ,k) * B(k,jj);
t1 = 0.0020, t2 = 5.0160, t3 = 0.1100, t4 = 4.9060
end fprintf( 'i = %d\n', i );enddisp( 'End of loop' );
Write a program in which the user tries to guess a number
picked by the computer. The number is picked between 1 and
10 and the user has at most three tries.
num = fix(10 * rand + 1);if num == 11, num = 10; end
for tries = 1:3, guess = input( 'Your guess? ' ); if ( guess == num ), disp( 'Congratulations!' ); break; end end if ( guess ~= num ), disp( 'You could not guess correctly' ); end
Compute the perimeter of a polygon whose size is specified by
N = input('Enter the polygon size: '); if N < 3 disp('It is not a polygon'); else i = 1; perimeter = 0; while (i <= N) edge_length = input([‘Length of edge ' num2str(i ) ': ']); if edge_length <= 0 disp('The length should be positive'); continue; end perimeter = perimeter + edge_length; i = i + 1; end end disp(['Perimeter: ' num2str(perimeter)]);
Vito J. Bruscato, in his capacity as the guardian of Victor Bruscato, appealsfrom the order of the Superior Court of DeKalb County, which granted summaryjudgment to Victor Bruscato’s psychiatrist, Derek Johnson O’Brien, M.D., in thismedical malpractice case.1 The superior court concluded that Bruscato’s malpracticeclaims were barred either by the application of the “impact rule” or o
MEDICATION INFORMATION FOR THOSE IN RECOVERY Minor illnesses such as cough, cold, allergy, stomach and respiratory flu, while not dangerous themselves, can presentproblems for people in recovery. Numerous over-the-counter (OTC) remedies often duplicate and overlap one another in effect, andpresent the consumer with a confusing array of options. Misinformation regarding OTC drugs can be tr