1 / 14

While Loops

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

ursala
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 11

  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:

  4. Today’s Topics • Reminders about while loops • Useful MATLAB functions for while loops • Examples of simple and complex while loops

  5. 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

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

  7. 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

  8. Example: Complex While Loop Task Steps 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 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.

  9. 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

  10. 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;

  11. Tic/Toc Example 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) %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

  12. 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

  13. Preview of Next Class • 2D Plots 1 • Basics of creating 2D plots using ‘plot()’ command • Plotting multiple curves on the same figure • Formatting tools for plots

  14. What’s Next? • Review today’s Quiz #11 • Open the in-class activity from the EEIC website and we will go through it together. • Then, start working on MAT-11 homework. • Prepare for the next class by reading about 2D Plots (Part 1)

More Related