+ 1

In java while loop works 10 - 0 and,, 'for' loop works reverse 0-10?

8th Jun 2017, 1:58 PM
prashant vyas
prashant vyas - avatar
8 Antworten
+ 2
Not per se, both loops can work both sides. int x = 8; while(x > 6){ x= x-1; } This makes x lower until x=7 (last time the loop runs) and gets lowered to 6. But also: int x = 8; while(x < 10){ x = x + 1; } works until x is incremented to 10. The same with for loops: for(i=0; i<2; i++){ alert("Hey!") } But it can also decrease the value of i: for(i=2; i>0; i--){ alert("Hey!") }
8th Jun 2017, 2:21 PM
Maart
Maart - avatar
+ 2
So what is work of for loop if both can work both sides?
8th Jun 2017, 2:23 PM
prashant vyas
prashant vyas - avatar
+ 2
you want to say that both loop work same to same.? ✍
8th Jun 2017, 2:31 PM
prashant vyas
prashant vyas - avatar
+ 2
ok ok english language little bit barrier I use Google translate now understand. I want to ask that this loop work same ? like brothers?
8th Jun 2017, 2:33 PM
prashant vyas
prashant vyas - avatar
+ 2
ok now I understand while loop automatically work for loop work with conditions we put it with details.
8th Jun 2017, 3:19 PM
prashant vyas
prashant vyas - avatar
+ 1
You CAN use a loop for a number above 10, for example: int x = 30; while(x > 25){ x--; } This works too
8th Jun 2017, 2:31 PM
Maart
Maart - avatar
+ 1
Yeah you could say that!
8th Jun 2017, 2:34 PM
Maart
Maart - avatar
0
At different times it's easier to use one another, but most of the time you can technically choose between both because they're both possible EDIT: Adding to that, you can use for/while loops above 10 and under 0 too, it's not just 0-10
8th Jun 2017, 2:25 PM
Maart
Maart - avatar