14-440-127 Lecture 5 Notes

14-440-127 Lecture 5 Notes - 14:440:127 Introduction to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
14:440:127– Introduction to Computers for Engineers Notes for Lecture 5 Rutgers University, Fall 2008 Instructor- Blase E. Ur 0.1 Loops One of the things computers are very good at is doing repetitive tasks. Even better than repetitive tasks, computers are excellent at performing tasks over and over that are slightly different than each other. 0.1.1 For Loops Sometimes, you’ll know exactly how many times you need to perform some action. Take the fol- lowing example: ”Display the numbers from 1 to 100, individually.” Rather than type ( disp(1); disp(2); disp(3). ..) and so on, you could just use a for loop. A for loop lets you specify that Matlab perform some repetitive action over and over, changing one variable each time: for i = 1:100 disp(i) end The for loop must begin with the word for , and end with the word end . You must also specify some variable, here called i , that is known as the index. The first time you go through the loop, i will be 1. The next time you go through the loop, i will be 2, and so on, until the loop runs one final time with i equal to 100. Here’s an example of how loops could have been used in the Biomedical project, to find some minimum value and then print out all the rows with that minimum value: loc = find(c(:,6) == min(c(:,6))); for i = 1:length(loc) fprintf(’Gene %s has %.0f’, d{loc(i),1}, c(loc(i),6)) end 0.1.2 While Loops While a for loop lets you repeat some segment of code a specified number of times, a while loop lets you repeat some segment of code until a particular condition is false. While the condition is true, Matlab will loop through that code. Indeed, a while loop won’t even start if the condition is not originally true. Each cycle through the loop is called an ”iteration.” Note that if the condition becomes untrue part way through an iteration, the particular iteration will finish and the loop will not stop until the condition is checked again. Also note that, like for loops and if statements, the while loop requires that you type ”end.”
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: If you add the integers 1 through n, what is the smallest n for which the sum is a 3 digit number? sum = 0; n=1; while(sum<100) sum = sum+n; n=n+1; end fprintf(’The smallest n is %.0f’,n) The while loop is also very useful when getting input from the user. (Think about why it’s necessary that we write the input statement twice): x = input(’Enter a number’) while(x>0) fprintf(’The square of your number is %.0f’,x^2) x = input(’Enter a number’) end 0.2 Review of Lectures 1-4 for Exam 1 0.2.1 Review of Lecture 1- Relational/Logical Operators
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

14-440-127 Lecture 5 Notes - 14:440:127 Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online