- 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
5 Answers
+ 4
And without any loop, here it is
int i=10;
step:
cout << i << endl;
if(i>1)
{
--i;
goto step;
}
+ 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
+ 1
this is beautiful. can someone show me how to achieve the same using step: with if statement but in Java
0
you can also use do-while :
int i=10;
do {
i--;
cout <<i <<endl
}
while (i != 0);
- 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);