Download presentation
Presentation is loading. Please wait.
Published byLaura McCarthy Modified over 8 years ago
1
Loops CS 103 February 23, 2004
2
Review FOR is a control construct that provides a loop for ii = 1:10 fprintf(‘%d \n’, ii) end FOR is a control construct that provides a loop for ii = 1:10 fprintf(‘%d \n’, ii) end FOR loops are great when you know exactly how many times the loop should run FOR loops are great when you know exactly how many times the loop should run To perform operations on an array of indeterminate length, try using: for ii = 1:length(x) To perform operations on an array of indeterminate length, try using: for ii = 1:length(x)
3
While Loops Syntax: Syntax: WHILE expression statements statementsend
4
While Loops WHILE expression statements statementsend Unlike a for loop, the expression in a while loop consists of a logical or relational test. As long as that test evaluates to true, the loop will continue Unlike a for loop, the expression in a while loop consists of a logical or relational test. As long as that test evaluates to true, the loop will continue
5
Example n = 1 while n < 100 fprintf(‘x = %3.0f\n’, n); n = n + 1; end
6
Indexing WHILE loops do not have their own built-in index! WHILE loops do not have their own built-in index! If your program needs an index, you’ll have to create it yourself. If your program needs an index, you’ll have to create it yourself. You’ll also have to increment it yourself. You’ll also have to increment it yourself.
7
Try It Out... Write a WHILE loop that prints the squares from 1 to 10 Write a WHILE loop that prints the squares from 1 to 10
8
Now Try This... Given the array x = [1 2 7 21 -5 6 9] write a WHILE loop that calculates and prints the square of each value in the array Given the array x = [1 2 7 21 -5 6 9] write a WHILE loop that calculates and prints the square of each value in the array Make this program robust by writing code that will allow me to change the value of x later and still have the code run. (HINT: use length(x) ) Make this program robust by writing code that will allow me to change the value of x later and still have the code run. (HINT: use length(x) )
9
Pitfall Avoidance Your logical or relational expression has to be able to handle the endpoints of your array properly Your logical or relational expression has to be able to handle the endpoints of your array properly Consider the following workarounds: n <= length(x) n < (length(x) + 1) Consider the following workarounds: n <= length(x) n < (length(x) + 1)
10
Application: Data Validation WHILE loops are extremely useful in making your programs robust. WHILE loops are extremely useful in making your programs robust. TRUE = 1; FALSE = 0; lambda = 0.00012097; got_input = FALSE; percentage = input('Please give me the percentage of activity. '); if percentage < 0 fprintf('Percentage cannot be negative.\n'); fprintf('Percentage cannot be negative.\n'); elseif percentage == 0 fprintf('The age of the object is infinity.\n'); fprintf('The age of the object is infinity.\n'); elseif percentage > 100 fprintf('Percentage cannot be greater than 100.\n'); fprintf('Percentage cannot be greater than 100.\n'); elseif imag(percentage) ~= 0 fprintf('Percentage cannot be complex.\n'); fprintf('Percentage cannot be complex.\n'); elseif sum(size(percentage)) > 2 fptinrf('Percentage must be a scalar\n'); fptinrf('Percentage must be a scalar\n');else got_input = TRUE; got_input = TRUE;end if ~got_input return returnEnd t = (1/lambda)*log(100/percentage); fprintf('The age of the object is %d years\n', t);
11
Robust Programming got_input = FALSE; percentage = input('Please give me the percentage of activity. '); while ~got_input if percentage < 0 if percentage < 0 fprintf('Percentage cannot be negative.\n'); fprintf('Percentage cannot be negative.\n'); elseif percentage == 0 elseif percentage == 0 fprintf('The age of the object is infinity.\n'); fprintf('The age of the object is infinity.\n'); elseif percentage > 100 elseif percentage > 100 fprintf('Percentage cannot be greater than 100.\n'); fprintf('Percentage cannot be greater than 100.\n'); elseif imag(percentage) ~= 0 elseif imag(percentage) ~= 0 fprintf('Percentage cannot be complex.\n'); fprintf('Percentage cannot be complex.\n'); elseif sum(size(percentage)) > 2 elseif sum(size(percentage)) > 2 fptinrf('Percentage must be a scalar\n'); fptinrf('Percentage must be a scalar\n'); else else got_input = TRUE; got_input = TRUE; end end if ~got_input if ~got_input percentage = input('Please give me the percentage of activity. '); percentage = input('Please give me the percentage of activity. '); end endend
12
Factorials N! is defined as 1*2*3*…*(N-2)*(N-1)*N N! is defined as 1*2*3*…*(N-2)*(N-1)*N n = input(‘n please: ‘); f = 1; for I = 2:1:n f=i*f;end fprintf(‘%d\n’, f);
13
Vector Storage Now, let’s alter our factorial program to store the values in a vector Now, let’s alter our factorial program to store the values in a vector n = input('n please: '); f(1) = 1; for i = 2:1:n f(i) = i*f(i-1); f(i) = i*f(i-1);end fprintf('f(%d) = %d\n', i, f(i));
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.