0

please help me out with this

how to rewrite the following program using while loop include <iostream.h> using namespace std; int main() { int k=6; while (k>=0) { cout<<k<<endl; if (k>4) cout<<2*k<<endl; cout<<k<<endl; k--; } return 0; }

18th Feb 2021, 5:55 AM
pingo931
pingo931 - avatar
14 Answers
+ 2
How did you expect the code to work? If you see a need for reuse of code (or blocks of code), then you write a function based on the loop workflow. You can afterwards use the function wherever you need.
18th Feb 2021, 6:27 AM
Ipang
+ 2
I don't know how to help you this way unfortunately Good luck! 👍
18th Feb 2021, 6:51 AM
Ipang
+ 1
You're already using while loop there! look at line 2 in function main ...
18th Feb 2021, 6:16 AM
Ipang
+ 1
Share the actual instruction here. There should be a criteria on rewriting the code. Like "rewrite this using blah so that it does blah etc etc"
18th Feb 2021, 6:45 AM
Ipang
0
I know but is there any way to reuse it differently?
18th Feb 2021, 6:22 AM
pingo931
pingo931 - avatar
0
Try "for" loop
18th Feb 2021, 6:27 AM
Nazeekk
Nazeekk - avatar
0
include <iostream.h> using namespace std; int main() { int k=6; for (; k >= 0; k--) { cout<<k<<endl; if (k>4) cout<<2*k<<endl; cout<<k<<endl; } return 0; }
18th Feb 2021, 6:28 AM
Nazeekk
Nazeekk - avatar
0
it's a quiz and it asks to rewrite this code using while loop how I'm going to put 'for' instead 😭💀
18th Feb 2021, 6:38 AM
pingo931
pingo931 - avatar
0
But you already have "while" loop😳😳
18th Feb 2021, 6:41 AM
Nazeekk
Nazeekk - avatar
0
I've never write any code using C++ lang so I have no idea how I'm going to rewrite it
18th Feb 2021, 6:41 AM
pingo931
pingo931 - avatar
0
include <iostream.h> using namespace std; int main() { int k=0 while (k<=6) { cout<<k<<endl; if (k>4) cout<<2*k<<endl; cout<<k<<endl; k++; } return 0; }
18th Feb 2021, 6:41 AM
Nazeekk
Nazeekk - avatar
0
Try this😳😳
18th Feb 2021, 6:41 AM
Nazeekk
Nazeekk - avatar
0
Try this out while(k--) { cout<<k<<endl; If(k>4)cout<<2*k<<endl: cout<<k<<endl; }
19th Feb 2021, 4:00 PM
Yogesh Vaishnav
Yogesh Vaishnav - avatar
0
This is going to be finite loop!! Can u post the question so it'll be easy to solve btw coming to your point u can use any of the loop either for or while that depends on you!! using for loop : for ( , k>=0 , k--) { Cout<<k; if (k>4) { Cout<<k*2 <<endl; } } Using while : While(k>0) { Cout<<k<<endl; If(k>4) { Cout<<k*2; K--; } }
20th Feb 2021, 3:49 AM
kreddyt
kreddyt - avatar