フィルターのクリア

Why is my while loop infinite?

4 ビュー (過去 30 日間)
Delaney Andersen
Delaney Andersen 2018 年 11 月 3 日
コメント済み: Walter Roberson 2018 年 11 月 4 日
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);

回答 (1 件)

dpb
dpb 2018 年 11 月 3 日
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 件のコメント
Delaney Andersen
Delaney Andersen 2018 年 11 月 4 日
nVal should increase by 1 until it equals the 'inpuTm' to create a vector 'pofN' with nVal number of values.
Walter Roberson
Walter Roberson 2018 年 11 月 4 日
Why not use a for loop?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by