+ 4
How do i write for loop downwards
6 ответов
+ 4
Martin Taylor
Thanks for the heads up, common slip-offs from copy/paste stuffs 🙏
+ 7
How do you define *downwards* ??
do you mean something like this :-
for(int I = 20; I>0;I--)
{
// Loop body
}
+ 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)
+ 4
See this course https://www.sololearn.com/learn/JavaScript/1140/
+ 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.
+ 2
Is intiliazation also used