+ 4

How do i write for loop downwards

9th Dec 2020, 11:18 AM
Maureen Muiru
Maureen Muiru - avatar
6 ответов
+ 4
Martin Taylor Thanks for the heads up, common slip-offs from copy/paste stuffs 🙏
9th Dec 2020, 12:19 PM
Ipang
+ 7
How do you define *downwards* ?? do you mean something like this :- for(int I = 20; I>0;I--) { // Loop body }
9th Dec 2020, 11:22 AM
Arsenic
Arsenic - avatar
+ 6
By adjusting the for...loop definition ... for( let i = 0; i < 10; i++ ) This goes upwards because <i> began with 0, the loop condition evaluates to true while <i> is less than 10, and we're incrementing <i>. for( let i = 10; i > 0; i-- ) This goes downwards because <i> began with 10, the loop condition evaluates to true while <i> is greater than 0, and we're decrementing <i>. (Edited)
9th Dec 2020, 11:28 AM
Ipang
9th Dec 2020, 11:25 AM
Matias
Matias - avatar
+ 3
Yes, initialization matters. As you can see, the initialization must be in accordance with the condition. If we set i = 10 in first example, or set i = 0 in second example, the loop will not run at all.
9th Dec 2020, 11:56 AM
Ipang
+ 2
Is intiliazation also used
9th Dec 2020, 11:33 AM
Maureen Muiru
Maureen Muiru - avatar