continue statement
Title
Question
<span style="line-height: 21.4286px;">I write a code :</span>
-->for i=1:5:54
-->if ((i/4)==0) then continue
-->else disp(i)
-->end end
output should be:
1
6
<span style="line-height: 1.42857;">11</span>
<span style="line-height: 1.42857;">21</span>
<span style="line-height: 1.42857;">26</span>
<span style="line-height: 1.42857;">31</span>
<span style="line-height: 1.42857;">41</span>
<span style="line-height: 1.42857;">46</span>
<span style="line-height: 1.42857;">51</span>
<span style="line-height: 1.42857;">but output comes out to be :</span>
<span style="line-height: 21.4285px;">
</span>
</span>
<span style="line-height: 21.4285px;"> 1. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 6. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 11. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 16. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 21. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 26. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 31. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 36. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 41. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 46. </span>
<span style="line-height: 21.4285px;"> </span>
<span style="line-height: 21.4285px;"> 51. </span>
<span style="line-height: 21.4285px;"> can you explain me, where I am wrong ?</span>
Scilab Iteration 5-6 min 0-10 sec
Answers:
The logic of the code is incorrect. It is displaying all the integers as the number divided by 4 is not equal to zero(i/4==0).
Use the modulo operator which checks for the remainders and skips the integers with the remainder zero.
Code:
clc;
clear all;
for i = 1:5:54
if (modulo(i,4)==0) then //Checks for the remainder 0
continue
else
disp(i)
end
end
output:
1.
6.
11.
21.
26.
31.
41.
46.
51
Use the modulo operator which checks for the remainders and skips the integers with the remainder zero.
Code:
clc;
clear all;
for i = 1:5:54
if (modulo(i,4)==0) then //Checks for the remainder 0
continue
else
disp(i)
end
end
output:
1.
6.
11.
21.
26.
31.
41.
46.
51
Correct code should be
clc;
clear all;
for i = 1:5:54
if (modulo(i,4)==0) then //Checks for the remainder 0
continue
else
disp(i)
end
end
clear all;
for i = 1:5:54
if (modulo(i,4)==0) then //Checks for the remainder 0
continue
else
disp(i)
end
end
then output would be
1.
6.
11.
21.
26.
31.
41.
46.
51.
Ravi Kumar B., SASTRA Deemed University, Thanjavur
Login to add comment