Clear Filters
Clear Filters

Why is my while loop infinite?

4 views (last 30 days)
Delaney Andersen
Delaney Andersen on 3 Nov 2018
Commented: Walter Roberson on 4 Nov 2018
I'm not sure why this loop is running infinitely; shouldn't it end as soon as nVal is equal to the value of inputM?:
clear;
clc;
inpuTm = input('Enter the value for m: ');
nVal = 2;
pofN = [0,1];
while nVal <= inpuTm
pNext = 2*(nVal-1)+(nVal-2);
pofN = [pofN,pNext];
end
disp(pofN);

Answers (1)

dpb
dpb on 3 Nov 2018
Yes, but neither nVal nor inpuTm are modified inside the while loop, so they remain unchanged and the loop either never executes or is an infinite loop depending upon the value of inpuTm.
  4 Comments
Delaney Andersen
Delaney Andersen on 4 Nov 2018
nVal should increase by 1 until it equals the 'inpuTm' to create a vector 'pofN' with nVal number of values.
Walter Roberson
Walter Roberson on 4 Nov 2018
Why not use a for loop?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!