Loop in MATLAB the best ever guide with examples

Page 1

01

matlabassignmenthelp.com

Loop in MATLAB The Best Ever Guide With Examples


Today's agenda Overview While Loop For loop The Nested Loops Loop Control Statements Break statement Continue Statement Conclusion

02


Overvie w

Most of the students are not well aware of the loops in MATLAB. Likewise, other programming languages MATLAB also has some loop. So, before we are getting into the loop in MATLAB, let’s understand what exactly the loop does in programming. With the help of the loop, you can execute the block of the statements many times. Thus the statement executed sequentially i.e., the first statement executed, the second one, and so on.

03


While Loop

04

The while loop works on the simple phenomena. It executes the statement continuously until the specific condition becomes false. Here is the syntax of while loop:While <expression> <statement> End In the while loop, the program statement repeatedly executes as long as the expression becomes false. In other words, the result contains the non zero elements or non-empty the condition becomes true, otherwise false. x = 20; % while loop execution while( a < 25 ) fprintf(‘value of x= %d\n’, a); x = x + 1; end


For loop

05

The for loop is the best loop in any programming language. In the for loop, you get a repetition control structure. It allows you to have full control to write a loop that needs to execute any number of times. The syntax of a for loop in MATLAB for index = values <programming statements> ‌ end Values: There can be following forms of value: And the statement executes until the index is greater than endval. Initval:step:endval — In this, the index increments by the value step on each iteration. And it decrements when the step is negative.


The Nested Loops

06

You can also use a loop inside another loop in Matlab. There are two types of nested loops in MATLAB. The first one is nested for loop, and the other one is nested while loop. Here is the syntax of for loop in MATLAB for m = 1: j for n = 1: k ; end end The syntax for a nested while loop statement in MATLAB is as follows while <expression> while <expression2> <statement> end end


mat;abassignmenthelp

Loop Control Stateme nts

The Loop control statement is used to make the change execution from the normal sequence of the loop. In this, all the automatically created objects in the scope destroyed when the scope leaves by the execution. Hereby the scope means where the variable is valid in MATLAB. For example, the scope of the variable remains in the loop from the beginning of the conditional code to the end of the code. The scope also tells MATLAB what to do when the conditional becomes false. Here in MATLAB, you find two types of loop control statements i.e., the break statement and the continue statement.


Break statement

08

With the help of the break statement, we can terminate the execution of for or while loops. If we put the break statement in any loop, then the statement which appears after the break statement is not executed. On the other hand, if we talk about the nested loops, the break only works in the loop in which it occurs. Then the statement automatically passes to the next loop Example a = 5; % while loop execution while (a < 10 ) fprintf(‘value of a: %d\n’, a); a = a+1; if( a > 10) % terminate the loop using break statement break; end


Continue Statement

09

In the continue statement, the control automatically passes the next iteration for both the for loop and the while loop. Most of the time, the continue statements work like the break statement in MATLAB. The continue statement puts force on the next iteration to take place instead of forcing the transmission. Example a = 5; % while loop execution while (a < 10 ) if a == 15 % skip the iteration a = a + 1; continue; end fprintf(‘value of a: %d\n’, a); a = a + 1; end.


FACEBOOK @matlanassignmenthe lp1

Follow Us on Social Media

TWITTER @matlabassignme5

PINTEREST @matlahassignmenthe lp3


WEBSITE

Contact us

www.matlabassignmenthelp.com

EMAIL ADDRESS info@marlabassignmenthelp.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.