Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Advantages of for and while loops?

Subject: Advantages of for and while loops?

From: Jeremy

Date: 15 Apr, 2010 22:15:39

Message: 1 of 1

Say you have a function f(x) that you wish to integrate in steps from a to b.

Two ways of writing this program would be:

syms x;
i=1;
while i<=10
    I=int(f(x),i-1,i)
    i=i+1;
end

syms x;
for i=1:10
    I=int(f(x),i-1,i)
end

Obviously the while loop is more code to write, but will it effect the efficiency of the program? I'm thinking that the while loop is going to take more time to compute, can anyone confirm this? What's the advantages of using a while loop in a situation like this, if any?

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us