In order to code HDL, how to avoid using break statements in loops

11 ビュー (過去 30 日間)
Life is Wonderful
Life is Wonderful 2022 年 11 月 11 日
編集済み: Life is Wonderful 2022 年 11 月 11 日
Since I'm seeking for HDL code that doesn't allow the break, continue statement, I need help with the following code logic to stop the break statement from being executed.
case 1:
clc;
eps = 5;
x = 1:5;
y = zeros(5,1);
fprintf('%10s|%10s|\n----------+----------+\n','n','y(n)');
n| y(n)| ----------+----------+
for idx = 1:length(x)
y(idx) = x(idx) + 1;
if(y(idx) > eps)
break;
end
fprintf('%10d|%10d|\n',idx,y(idx));
end
1| 2| 2| 3| 3| 4| 4| 5|
case : 2
clc;
eps = 5;
x = 1:5;
y = zeros(5,1);
fprintf('%10s|%10s|\n----------+----------+\n','n','y(n)');
n| y(n)| ----------+----------+
for idx = 1:length(x)
y(idx) = x(idx) + 1;
if(y(idx) > eps)
idx = 1;
end
fprintf('%10d|%10d|\n',idx,y(idx));
end
1| 2| 2| 3| 3| 4| 4| 5| 1| 2|
Because more iterations are being executed, case 2's results are incorrect.
Thank you!!

採用された回答

Life is Wonderful
Life is Wonderful 2022 年 11 月 11 日
編集済み: Life is Wonderful 2022 年 11 月 11 日
I think i have the solution ,
x = 1:5;
eps = 5;
y = zeros(1,1);
fprintf('%10s|%10s|\n----------+----------+\n','n','y(n)');
n| y(n)| ----------+----------+
idx = 1;
while (y < eps)
y = x(idx) + 1;
fprintf('%10d|%10d|\n',idx,y);
idx = idx + 1;
end
1| 2| 2| 3| 3| 4| 4| 5|
Any fresh suggestion for improvement is welcome, and I am open to it.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHDL Coder についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by