Info

この質問は閉じられています。 編集または回答するには再度開いてください。

A statement under "for" gets skipped during the last loop

1 回表示 (過去 30 日間)
Rahul Raman R
Rahul Raman R 2019 年 8 月 29 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
function coded = caesar (v,s)
coded=v;
count=0;
for ii = v+s
count=count+1;
if (ii>126)
ii=32+(ii-126-1);
else if (ii<32)
ii = 126-(32-ii-1);
end
coded(count)=ii;
end
end
The above given code is made for a decoder and an encoder so when i execute it, the code works fine until it reaches the end of the string (v+s) , at the end it seems the variable ii is assigned a value "the right one" but the statement " coded(count)=ii " does not run (the loop skips it the last iteration) , is there something i am missing here.
the input i gave is mentioned below
caesar('~',1)
and coded returns the value '~' instead of ' '

回答 (1 件)

KSSV
KSSV 2019 年 8 月 29 日
編集済み: KSSV 2019 年 8 月 29 日
YOu are changing the loop index ii inside the if...else statements.......that is now allowed...
function coded = caesar (v,s)
coded=v;
count=0;
for ii = v+s
count=count+1;
jj = ii ;
if (ii>126)
jj = 32+(ii-126-1);
else if (ii<32)
jj = 126-(32-ii-1);
end
coded(count)=jj;
end
end

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by