+ 3

Please explain this code

Please explain step by step on how this code works. https://code.sololearn.com/c63DK9nUSIY2/?ref=app

29th Jan 2018, 4:40 PM
Vinyas Amin
Vinyas  Amin - avatar
3 Respostas
+ 8
first define four arrays have 5 elements each (start from index 0 to index 4). a for loop works on life array through a nested while loop. starts from the first element i=0 => life[0]=50, the condition of the while loop is true (since 50>0) so its statement will be executed which is read as : the next value for life[0] is equal to the present value mines damage[0](the first element of the damage array which is 5) so life[0]=50-5=45, and print the message. then checks the while condition again (45>0) true, so repeat the statement again, life[0]=45-5=40, print the message. check the condition again, until life[0]=0 then the while loop stops and breaks to the next line outside the while loop which is i=i+1, which is read as increase the value of i by 1(0+1=1) =>> now i holds the value of 1. return to the for loop since i<array length now the for loop works on the second life array element (life[1]) through the while loop. check the while loop condition life[1]>0 and since it's true execute the statement and repeat until life[1]=0, while loop stops, i increase by 1 (now holds the value of 2) , and go back to for loop. repeat the procedure until i>4 then for loop stops and since there is nothing to do the code ends.
29th Jan 2018, 11:15 PM
Mazin Ayash
Mazin Ayash - avatar
+ 4
Thanks to all of those who had answered
30th Jan 2018, 2:11 PM
Vinyas Amin
Vinyas  Amin - avatar
29th Jan 2018, 11:40 PM
Daniel
Daniel - avatar