+ 5
How do I loop backwards?
For example forward loop is done thus; function move(){(if pos >= 1050) {clearInterval (a); } else {pos++; moving.style.left = pos+ 'px';}}
4 odpowiedzi
+ 4
start from where you stop to where you begin by decreesing the initialization value
+ 4
Pls can you drop an example?
+ 2
Can you please tell me which language you are using?
For C++, Java, PHP, JavaScript, C# and C this works:
for (int i = 5; i > 0; i--){
// i will always be one smaller
}
For Python:
for i in range(5, 1, -1):
print(str(i))
#Output 5 4 3 2
NOTE: the "-1" is necessary! (Step)
Backwards iteration in Ruby: https://stackoverflow.com/questions/2070574/is-there-a-reason-that-we-cannot-iterate-on-reverse-range-in-ruby
For kotlin:
for (n in 100 downTo 1) {
//Do something
}
For Swift:
for index in stride(from: 5, to: 1, by: -1) {
print(index)
}
//prints 5, 4, 3, 2
Hope this helps!
If not, please tell me which language you need it for!
0
//to loop backward in PHP is very easy. So, use this example:
<?php
function samp(){
$example = 10;
while($example > 1){
$example--;
echo $example."<br>";
if($example ==1){
echo "time up!";
}
}
}
samp();
?>