1 / 11

For Loops 1

For Loops 1. ENGR 1181 MATLAB 8. For Loops and Looped Programming in Real Life.

meris
Download Presentation

For Loops 1

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. For Loops 1 ENGR 1181 MATLAB 8

  2. For Loops and Looped Programming in Real Life Looping within programs has long been a useful tool for completing mundane tasks over and over again. The focus of today’s lesson is fixed number looping although later you will learn about indefinite looping (while looping). Internal combustion engines use a combination of these loops for controlling the spark plugs and valves during a four stage cycle. The engine completes these four stages (for loop) while the car is engine is running indefinitely (while loop, to be learned later).

  3. Today's Learning Objectives • After today’s class, students will be able to: • Explain the iterative nature of loops. • Use loops properly for repetitive processes, including the utilization of loop indices as counters and variables.

  4. What is a Loop ? • A loop allows a group of commands in a program to be repeated. • Each repetition of the loop is called a pass. • Eitherthe number of passes can be fixed. • Orthe loop can be terminated after some condition is satisfied.

  5. Examples of basic for–end Loops Output: 1 2 3 for k = 1:3 disp(k) end

  6. Examples of basic for–end Loops Output: 5 10 15 20 X=[5, 10, 15, 20]; fori = 1: length(X) disp(X(i)) end

  7. for–end loops with vectors Create the vector v = [2 4 6 8]. Calculate and print the square of each element. Output: vs = 4 vs = 4 16 vs = 4 16 36 vs = 4 16 36 64 v = [2 4 6 8] ; for k = 1 : 4 vs(k) = v(k)^2 end

  8. Additional Example • Calculate the factorial of integers 1 through 5 inclusive. clc clear %This script file calculates the factorial of a number %Declare the number you wish to calculate the factorial for n = 5; %Calculate factorial using a for loop x = 1; for i = 1:n x = x*i; end fprintf(‘The factorial of %i is %i\n’,n,x)

  9. Important Takeaways • A loop allows a group of commands to be repeated. • A for loop repeats a specified number of times and the loop index variable determines when the for loop ends. • The for loop index variable can also be used to index values from arrays.

  10. Preview of Next Class • For Loops 2 • Review of basic for loops • Nesting conditional statements in for loops • ‘Break’ and ‘Continue’ Loops

  11. What’s Next? • Review today’s Quiz #08 • Open the in-class activity from the EEIC website and we will go through it together. • Then, start working on MAT-08 homework. • Prepare for next class by reading about computer problem solving in MATLAB.

More Related