# What is The Syntax of Do While in Matlab | How to Use it in Coding

There is no loop as do while Matlab, but other programming languages, such as C, C++ has this loop to execute a specific function in the program. But rather than using the do-while loop in Matlab, there are two kinds of the loop that are utilized as do operations. “For loop” can be used, if a programmer is sure about how many times he or she requires to perform a specific task. However, if a programmer is not sure about the number of tasks that he has to perform, the programmer can use “while loop” within the program. Within the loop statement, one can write a condition and repeat the statements of specific code and decrement/increment the variable as per requirement.

The syntax which is used for writing the while loop program is “while” whereas, at the end of the program, we require to write the code ‘end’ to stop the functioning of the loop. This post will help you to understand how to use do while in Matlab program to increment/decrement the variable in code.

### How to use do while Matlab loop?

To write a while loop statement in Matlab, a programmer always requires to consider the following factors:

• The initial condition is used to limit the execution time of the loop.
• The second statement is used to know the expected output result of the program.
• The third factor is used to increase the variable of the given loop. If a programmer does not write the line of increment, then the loop will run for an infinite time.

### Syntax of do while in Matlab

As we know, do while in Matlab is a simple loop that is used to evaluate the program at least once. Therefore, the simplest method to execute the program is to start the program with a while loop that has the true statement and reevaluate the given condition until the loop does not end.

syntax:

condition = true;

while condition       %execute  a program

condition = …        %write while test condition here.

end

OR

while(condition)

Statement No 1

.

.

Statement No n

Syntax for Increment/decrement loop variable

### Example of do while Matlab program

Example 1:

In the given example, let’s assume a variable x. The initial value can be assigned to the variable x is 4. After applying a particular condition with the while loop statement (x < = 9), the loop will run the value like 4, 5, 6, 7, 8, 9. Here the statement just shows the value of x; therefore, it can show the output as 4, 5, 6, 7, 8, 9.

Program:

x = 4

while(x < = 9)

disp(x)

x = x + 1;

end

Output:

X = 4

4

5

6

7

8

9

Example 2:

This example has a square function, which means this program will find the square of values till 6. Here the program has “var,” which is used to implies the variable name. The value of var is 2, and this will execute the value from 2 to 6.

Program:

var = 2

while(var <=6)

sq=var*var;

disp(sq)

var = var + 1;

end

Output:

Var = 2

4

9

16

25

36

#### This example includes two types of operations: one for describing the odd numbers, and the other is used for even numbers. To check whether the number is the odd number or even number, we have used to different kinds of loops.

Program:

var = 1

disp(‘numbers are odd’)

while(var <= 8)

disp( var )

var = var + 2 ;

end

var = 0

disp(‘numbers are even’)

while(var <= 8)

disp(var)

var = var + 2;

end

Output:

var= 1

Numbers are odd

1

3

5

7

var= 0

Numbers are even

2

4

6

8

Example 4:

In example 3, we have started the numeric value from the origin, but this time, we will use a while loop to alter the range of the numeric values. In this example, we have taken the number start from 21 to 45. We have two different variables, where “var” is utilized for initialization, and “end” is utilized for the end of the program.

Program:

% odd numbers between 21 to 45

var = 21

disp(‘numbers are odd’)

while(var <= endat)

disp( var )

var = var + 2 ;

end

var = 20

endat = 45

disp(‘numbers are even’)

while(var <= 45)

disp( var )

var = var + 2;

end

Output:

var= 21

Numbers are odd

21

23

25

27

29

31

33

35

37

39

41

43

45

var=20

endat= 45

Numbers are even

20

22

24

26

28

30

32

34

36

38

40

42

44

Example 5:

With the help of a while loop, we can generate several numbers of applications and series. In the following example, we have generated a number series as per the previous example’s outcome.

Program:

var = 0

endat= 5

x = 0

disp(‘series of number’)

while(var <= endat)

%disp(var);

x = x+ var

var = var + 1 ;

end

Output:

var= 0

endat=5

x=0

Series of numbers

x=0

x=1

x=3

x=6

x=10

x=15

### Conclusion

In this post, there are two main loops that are used in a do while in Matlab coding program, and that is “while” and “for” loop. The “while” loop is utilized in the program where the programmer does not have any idea for repetition statements. Whereas “for” is used when a programmer sure about the repetition of the statement. The above-mentioned examples have shown the use of a while loop in several ways. It can enhance program efficiency and also reduce the program’s complexity.You are facing any problem related to the Matlab assignment; then, you can avail of our experts’ help for Matlab assignment help. We have the number of experts who can provide you high-quality data in the slotted time. We are also renowned for our best services that are available at reasonable prices. So, take our services and get relaxed from your Matlab online help.