+ 1
In java while loop works 10 - 0 and,, 'for' loop works reverse 0-10?
8 Réponses
+ 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!")
}
+ 2
So what is work of for loop if both can work both sides?
+ 2
you want to say that both loop work same to same.? ✍
+ 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?
+ 2
ok now I understand while loop automatically work for loop work with conditions we put it with details.
+ 1
You CAN use a loop for a number above 10, for example:
int x = 30;
while(x > 25){
x--;
}
This works too
+ 1
Yeah you could say that!
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