Why is my while loop infinite?
Mostrar comentarios más antiguos
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);
Respuestas (1)
dpb
el 3 de Nov. de 2018
0 votos
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 comentarios
Delaney Andersen
el 4 de Nov. de 2018
dpb
el 4 de Nov. de 2018
'Pends on what you're trying to accomplish...probably incrementing nVal in the loop I'd guess, but you don't indicate what you expect the result to be, specifically.
Delaney Andersen
el 4 de Nov. de 2018
Walter Roberson
el 4 de Nov. de 2018
Why not use a for loop?
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!