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.
Syntax:
while expression
statements
end
Example 1:
Matlab
%MATLAB code to illustrate
%for loop
count=0;
while (count < 3)
fprintf('Hello From GeekforGeeks\n');
count=count+1;
end
Output:
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.
Syntax:
for initial value:step value:final value
statements
end
or
for initial value:final value
statements
end
Example 2
Matlab
%MATLAB code to illustrate
%for loop
for i = 1:5
fprintf('%d ',i)
end
Output:
1 2 3 4 5
Example 3
Matlab
%MATLAB code to illustrate
%for loop
for i = 1:2:5
fprintf('%d ',i)
end
Output:
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).
Example 4
Matlab
%for iterator_variable = array
for i =[1 2 3 4]
fprintf('%d ',i)
end
Output:
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.
Example 5
Matlab
%MATLAB code to illustrate
%how to iterate through strings
String = 'GeeksforGeeks'
for i = 1:length(String)
fprintf('%c ',String(i))
%disp(String(i))
end
Output:
G e e k s f o r G e e k s
Similar Reads
Matlab - Matrix A Matrix is a two-dimensional array of elements. In MATLAB, the matrix is created by assigning the array elements that are delimited by spaces or commas and  using semicolons to mark the end of each row. Now let's have a glance at some examples to understand it better. Syntax: a = [elements; element
4 min read
MATLAB - Variables Prerequisite: Getting Started with MATLAB A variable in simple terms is a storage place that has some memory allocated to it. Basically, a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations that can be a
3 min read
MATLAB - Break Statement Break statement in MATLAB is used for breaking out of an iterative loop, for or while loop. The break in MATLAB is similar to the break statements in other programming languages such as C, C++, Python, etc. We will see how to break out of a single for or while loop and the nested implementation of t
2 min read
How to Create a Matrix From a Nested Loop in MATLAB? Matrices are 2-dimensional arrays that store numeric or symbolic data. It is convenient to create them with the help of nested loops as the outer loop creates the elements along one dimension and the inner loop creates the elements along the second dimension. In this article, we will see how to crea
2 min read
MATLAB - Read Words in a File in Reverse Order MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It allows matrix manipulations, plotting of functions, implementation of algorithms, and creation of user interfaces Suppose we are given a text file containing the following words "I STUDY F
2 min read