How To Apply WHILE LOOP in MATLAB

In previous post on MATLAB Tutorial, we have discussed about How to store multiple matrix with single variable in MATLAB with its matlab program. Now in Post-19 of Module-01, we will discuss about how to apply WHILE loop in matlab. 


To understand this problem, we are calculating factorial of any number, MATLAB Program for which has been shown in Figure-1.

Figure.1 - MATLAB Program to apply WHILE in MATLAB.

A number has been loaded in variable 'n' with the help of command written in line-4. This number also has been copied in variable 'factorial'. Condition written in line-6 will operate until n is greater than 1. A decrements of number is must in 'while' loop so that while condition may become false after some time otherwise program may go into infinite loop. Decrements has been done with the help of command written in line-7. 'factorial' has been multiplied with current value of number which update the value of 'factorial'. This loop is repeated until while condition does not becomes false. The output of above matlab program on command window has been shown in figure-2.

Figure.2 - Command Window output of matlab program to apply WHILE loop.

This program has been called by typing its name 'mod0119' on command window. After execution of program a message is displayed on command window asking about a number. When user type number '5' it display the factorial of this number after a message.

Video Tutorial : How To Apply While Loop in MATLAB By INFO4EEE


Brought to you by INFO4EEE (Website, Channel, and Classes).

No comments

Powered by Blogger.