1 / 12

While Loops

While Loops. ENGR 1181 MATLAB 10. While Loops in Real Life.

sgrube
Download Presentation

While Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. While Loops ENGR 1181 MATLAB 10

  2. While Loops in Real Life Revisiting the previous example of the combustible engine from our first discussion of for loops (fixed number of runs), we can apply this example to while loops (indefinite number of runs). The fixed cycle of four states is the for loop, which runs indefinitely while the engine is on.

  3. Today's Learning Objectives • After today’s class, students will be able to: • Explain the proper application of while loops. • Use external inputs (e.g., tic, toc) for real time programming.

  4. Reminders About While Loops • While loops must have a conditional expression, and the conditional expression must have at least one variable. • Conditional variables must be "initialized" before the loop • The conditional variable must advance with each pass. (Otherwise the loop will continue indefinitely!) • ‘Ctrl + C’ will terminate execution of an indefinite loop. • Like for loops, must have an end

  5. a=0 while a<10 a=a+1if (a==5) continueenddisp(a)end Example: Simple While Loop Note: • conditional variable is initialized • variable advances each time • one end corresponds to if • one end corresponds to while 1234678910

  6. Function: rem() This function checks for and returns a remainder when dividing x by y. rem(x,y) Ex: rem(x,3)will check if the value of xis divisible by 3. Ex: rem1 = rem(23,5) rem1 = 3

  7. Write a program that will use a while loop to find the smallest number divisible by both 3 and 5, with the square of the number greater than 3325. Initialize variables: sq & n Loop while sq < 3325 Advance n by 3 in each pass Check if n is divisible by 5 If yes, square the n Repeat loop until sq >3325. Loop stops & n value will give us the smallest integer to meet conditions Example: Complex While Loop • Task • Steps

  8. Example: Complex While Loops n=0; sq=0; %initialize variables: # & square whilesq<=3325 %loop until square >3325n=n+3 %advance # by 3 each pass if rem(n,5)==0 %check if # is divisible by 5sq=n^2; %if yes, square #end %end if statementend %end while loop disp(n) %display the # that meets criteria

  9. Function: ‘tic’ and ‘toc’ • tic and toc are timing functions that monitor elapsed time in your program • tic starts a stopwatch that runs in the background • toc returns the elapsed time since the last tic command • The value of toc can be assigned to a variable • Ex: time1 = toc;

  10. N=input('\nPlease enter a large value for N: '); ticfori=1:N sqroot= sqrt(i);end SqrtTime=toc; fprintf('\n\nIt took MATLAB %f seconds to calculate %i square roots\n', SqrtTime, N) Tic/Toc Example %obtain value for N %start stopwatch%loop to calculate all of the square roots %obtain elapsed time %statement reporting how long it took to calculate the sq roots

  11. Important Takeaways • While loops need a conditional statement and conditional variables must be initialized • While loop will run indefinitely until condition is met • ‘rem()’, ’tic’, and ‘toc’ are very useful tools

  12. What’s Next? • Review today’s Quiz #10 • Open the in-class activity from the EEIC website and we will go through it together. • Then, start working on MAT-10 homework.

More Related