- 1

Various methods of implementing loop to print reverse 10 to 1, hope it helps.

output : 10 9 8 7 6 5 4 3 2 1

9th Dec 2016, 6:45 AM
Morpheus
Morpheus - avatar
5 Antworten
+ 4
And without any loop, here it is int i=10; step: cout << i << endl; if(i>1) { --i; goto step; }
9th Dec 2016, 7:43 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 3
In java there is not goto function. Yes it is reserved keyword, maybe for future use. But there is label you can use with continue and break. As continue and break work only in loop. You have to use loop for label also, here it is public class Program { public static void main(String[] args) { int i=10; step: for(;;) { System.out.println(i); i--; if(i<1) break; else continue step; } } } Hope it help you
9th Dec 2016, 9:57 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
this is beautiful. can someone show me how to achieve the same using step: with if statement but in Java
9th Dec 2016, 8:51 AM
Bugingo K. Daniel
Bugingo K. Daniel - avatar
0
you can also use do-while : int i=10; do { i--; cout <<i <<endl } while (i != 0);
9th Dec 2016, 8:49 AM
Jack Willson
Jack Willson - avatar
- 1
1.using for loop in c++, we have int i; for(i=10 ; i>0 ; i--) { cout<<i<<endl; } using while, we have int i=10; while(i>0) { cout<<i--<<endl; } using do-while we have int i=10; do { cout<<i--<<endl; }while(i>0);
9th Dec 2016, 6:48 AM
Morpheus
Morpheus - avatar