EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial do while loop in Matlab
 

do while loop in Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated June 16, 2023

do while loop in Matlab

 

 

Introduction to do while loop in Matlab

Matlab has no do-while loop like c programming, cpp programming, and other programming languages. But instead of using do while loop works powerfully in Matlab. In Matlab, mainly two loops are used to do operations. If we are sure how many times we need to perform a particular task, then for loop is used. And if we are unsure how often we want to perform a particular task, then a while loop is used. Inside the loop, we can write condition and repetition statements of particular programs and increment/decrement of variables.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

The syntax used to write the while loop in the program is the while’ command; at the end, we must write the ‘end’ command to stop the loop.

How do while loop works in Matlab?

We must always consider three parameters to write a while loop in Matlab.

  • The first condition limits the loop at the time of execution.
  • Second parameter statements mean what the expected output is.
  • The third parameter is the incrementing loop variable. If we miss the increment line, the loop will execute infinite times.

Syntax:
while(condition)
Statement1
Statement2
.
.
Statement n
Increment loop variable syntax

Examples of do while loop in Matlab

Given below are the examples of do while loop in Matlab:

Example #1

In this example, let us consider one variable a. The initial value assigned to a is 2. After applying condition ( a < = 5) along with the while loop, the loop will execute for values 2, 3, 4, 5. And here statement just displays the value of a. Therefore, it will display output as 2, 3, 4, 5.

Code:

a = 2
while(a <= 5)
disp(a)
a = a + 1;
end

Output:

do while loop in Matlab1

Example #2

The second example is a square function. In this example, we will find the square of values till 5. Here var is the variable name. The value assigned to var is 1, varying from 1 to 5.

Code:

var = 1
while(var <=5)
op=var*var;
disp(op)
var = var + 1;
end

Output:

do while loop in matlab 2

Example #3

In this example, there are two different operations, one is even-numbered, and the second is odd numbers. We have used two different while loops to find out even and odd numbers.

Code:

var = 0
disp('even numbers')
while(var <= 10)
disp(var)
var = var + 2;
end
var = 1
disp('odd numbers')
while(var <= 10)
disp( var )
var = var + 2 ;
end

Output:

two differnt while loops

Example #4

In previous examples, we start the problem from the origin, but we can change the range of problems by using a while loop. In this example, we consider numbers from 41 to 65. Here we have used two variables. Var is used for the start, and end at is used for the end of the range.

Code:

% even numbers between 41 to 65
var = 40
endat = 65
disp('even numbers')
while(var <= 65)
disp( var )
var = var + 2;
end
var = 41
disp('odd numbers')
while(var <= endat)
disp( var )
var = var + 2 ;
end

Output: 

used variables

Example #5

We can create various number series and applications using a while loop. In this example, we created a series of numbers by considering the output of previous iterations.

Code:

% addition of previous numbers
var = 0
endat= 10
op = 0
disp('number series ')
while(var <= endat)
%disp(var);
op = op + var
var = var + 1 ;
end

Output:

do while loop in Matlab3JPG

Conclusion

The ‘for’ loop and the ‘while’ loop are two main loops in any programming language. The’ while’ loop is used if the repetition statement’s range is unknown. The above examples show that while loop can be used in various ways with different parameters. It improves the program’s efficiency and reduces the problem’s complexity.

Recommended Articles

This has been a guide to do while loop in Matlab. Here we discuss the introduction; how does a while loop work in Matlab? Along with different examples and code implementation. You may also have a look at the following articles to learn more –

  1. Log Plot Matlab
  2. Random Number Generator in C#
  3. Vectors in Matlab
  4. Bitwise Operators in JavaScript
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW