MATLAB – Loops
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984.It is written in C, C++, Java. It allows matrix manipulations, plotting of functions, implementation of algorithms and creation of user interfaces.
- While Loop: While loop works same as it does in other common languages like python, java etc. But here syntax varies from language to language. While loop is used to execute a block of statements repeatedly until a given a condition is satisfied. And when the condition becomes false, the line immediately after the loop in program is executed.
while expression statements end
Hello From GeekforGeeks Hello From GeekforGeeks Hello From GeekforGeeks
- For Loop: For loops are used for sequential traversal. As syntax varies from language to language. Let us learn how to use for loop for sequential traversals.
for initial value:step value:final value statements end
for initial value:final value statements end
1 2 3 4 5
1 3 5
We have one more way of using for loop, that is used to access array elements. Here we assign an array directly to the for loop to access its elements through the iterator variable (i.e., i or j etc).
1 2 3 4
Iterating through strings is same as iterating through a range of numbers. Here we use length() function to provide final value in for loop, and we can also use disp() function to print the output.
G e e k s f o r G e e k s