0

What is difference between the two js in for loop j= i+1 and j=1

When we have nested for loops and we have var i=0 and var j=1 or var j=i+1.what is the difference between the two js.since i=0 and 0+1=1 .Does that not mean j=1 in both cases?

28th Jun 2022, 2:52 PM
steve Purpose
steve  Purpose - avatar
4 Answers
+ 1
When the outer loop executes for the first time 'j = 1' for first iteration of inner loop but, on the second execution of outer loop 'i' value will change which will result change in value of 'j'
28th Jun 2022, 3:26 PM
Random
Random - avatar
+ 1
If <i> was the outer loop counter and <j> was the inner loop counter, then j = i + 1 means value of <j> will vary depending on value of <i>. Whereas j = 1 means <j> value doesn't depend on other variable's value. <j> gets an independent value (integer literal), and if such assignment is placed in a loop, then variable <j> will always get the same value assigned in each loop iteration.
28th Jun 2022, 3:28 PM
Ipang
+ 1
Ipang nice answer
28th Jun 2022, 3:30 PM
steve Purpose
steve  Purpose - avatar
0
Snehil Pandey you didn't understand me properly.Thanks all the same
28th Jun 2022, 3:31 PM
steve Purpose
steve  Purpose - avatar