saving . . . saved use of while ststement has been deleted. use of while ststement has been hidden .
use of while ststement
Title
Question
To display all odd numbers from 1 to 25
i=1;
while(i<=25)
i=i+2;
disp(i)
if (i==25) 
then 
break

end end 

this displays all number 3 to 25 , except '1'
where i am doing wrong can you help?


Scilab Iteration 05-06 min 20-30 sec 06-05-20, 5:15 p.m. smitak

Answers:

You seem to be displaying, starting from i=2, as a result of the statement i=i+2.
06-05-20, 6:51 p.m. kannan
<pre style="font-family: Monospaced; font-size: 11px;"><span style="color:rgb(0,0,0);">i</span><span style="color:rgb(92,92,92);">=</span><span style="color:rgb(188,143,143);">0</span><span style="color:rgb(255,170,0);">:</span><span style="color:rgb(188,143,143);">12</span><span style="color:rgb(0,0,0);">;</span> <span style="color:rgb(160,32,240);">while</span> <span style="color:rgb(74,85,219);">(</span><span style="color:rgb(0,0,0);">j</span><span style="color:rgb(92,92,92);"><=</span><span style="color:rgb(188,143,143);">25</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(0,0,0);">j</span><span style="color:rgb(92,92,92);">=</span><span style="color:rgb(188,143,143);">1</span><span style="color:rgb(92,92,92);">+</span><span style="color:rgb(188,143,143);">2</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(0,0,0);">i</span><span style="color:rgb(0,0,0);">;</span> <span style="color:rgb(50,185,185);">disp</span> <span style="color:rgb(74,85,219);">(</span><span style="color:rgb(0,0,0);">j</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(160,32,240);">if</span> <span style="color:rgb(74,85,219);">(</span><span style="color:rgb(0,0,0);">i</span><span style="color:rgb(92,92,92);">==</span><span style="color:rgb(188,143,143);">12</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(160,32,240);">then</span> <span style="color:rgb(95,158,160);">break</span> <span style="color:rgb(160,32,240);">end</span> <span style="color:rgb(160,32,240);">end</span></pre><pre style="font-family: Monospaced; font-size: 11px;"><span style="color:rgb(160,32,240);">this gives output but not coming out of loop ,Break not working here</span></pre>
07-05-20, 2:43 p.m. smitak
07-05-20, 2:47 p.m. smitak

Login to add comment


Can't understand what you have written
07-05-20, 3:21 p.m. kannan


In while statement, you have put a condition that your code should not display values more than 25, so no need to write if statement again. Also, if you interchange the line 'disp(i)' with the line given above, you'll get the desired result.

So the code should be as follows:
i=1;
while(i<=25)
disp(i)
i=i+2;
end

08-05-20, 10:53 a.m. rashpat93


Log-in to answer to this question.