110 likes | 122 Views
Are you struggling with loop in MATLAB? If yes, then here is the best ever presentation of loop in MATLAB. Watch this presentation till the end to know everything about MATLAB.
E N D
Loop in MATLAB The Best Ever Guide With Examples 01 matlabassignmenthelp.com
Today's agenda Overview While Loop For loop The Nested Loops Loop Control Statements Break statement Continue Statement Conclusion 02
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. Overview 03
While Loop 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 04
For loop 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. valArray — In this, the program creates a column vector index from subsequent columns of the array. valArray on each iteration. 05
The Nested Loops 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 06
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. Loop Control Statements mat;abassignmenthelp 07
Break statement 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 end. 08
Continue Statement 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. 09
FACEBOOK TWITTER PINTEREST @matlanassignmenthelp1 @matlabassignme5 @matlahassignmenthelp3 Follow Us on Social Media
WEBSITE www.matlabassignmenthelp.com EMAIL ADDRESS info@marlabassignmenthelp.com Contact us