+ 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';}}

30th Jun 2019, 12:07 PM
Godswill francis
Godswill francis - avatar
4 odpowiedzi
+ 4
start from where you stop to where you begin by decreesing the initialization value
30th Jun 2019, 12:46 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
Pls can you drop an example?
30th Jun 2019, 1:39 PM
Godswill francis
Godswill francis - avatar
+ 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!
30th Jun 2019, 2:38 PM
Paul Grasser
Paul Grasser - avatar
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(); ?>
1st Jul 2019, 11:36 PM
AZZEETECH INFORMATION TECHNOLOGY
AZZEETECH INFORMATION TECHNOLOGY - avatar