+ 1

Loop and break

Hey fellow learners. I have a question regarding the exercise solution below. It is correct but I don't fully grab the logic behind it. At the end of the exercise the "+=2" part confuses me. What makes it possible that "depth" stops at the right moment and not exeeding "0" again to restart the whole loop? It can't be the "break" right? Because that statement came before the "+="? The snail climbs up 7 feet each day and slips back 2 feet each night. How many days will it take the snail to get out of a well with the given depth? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will re function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = 0; for (;depth>0;) {depth -=7; day ++; if(depth <= 0) break; depth+=2;} }

7th Feb 2021, 11:00 AM
SuperKitsune94
SuperKitsune94 - avatar
24 ответов
+ 3
SuperKitsune94 Actual logic is +7 climbs and -2 down but you applied here reverse engineering. Either you can solve by using new variable and compare with depth or you can do like as you did. Using new variable logic will be like this function main() { var days = 0; var d = 0; while(true) { d += 7; days++; if (d >= depth) break; d -= 2; } }
7th Feb 2021, 11:17 AM
A͢J
A͢J - avatar
+ 3
SuperKitsune94 See you started from depth and coming down means you already climbed and now coming down but problem is not like that. Problem says snails climbs 7 feet up and 2 feet down. So snails is on floor and trying to climbs so started journey from 0. So here I have taken a new variable with 0 because snails is on floor and going to climbs. After break loop is not possible because break throw you outside the loop.
7th Feb 2021, 11:46 AM
A͢J
A͢J - avatar
+ 3
SuperKitsune94 d -= 2 means d = d - 2 it means you are assigning the value of (d - 2) to d After using break d - 2 will not work. You can see example here https://code.sololearn.com/WYKVi0CE5nP5/?ref=app
7th Feb 2021, 11:58 AM
A͢J
A͢J - avatar
+ 3
SuperKitsune94 Take rest and give some time and think again why d -= 2 should be there even after break loop.
7th Feb 2021, 12:23 PM
A͢J
A͢J - avatar
+ 2
SuperKitsune94 There should be +7 and -2 because 1st day climbs 7 feet and next day down 2 feet then again 7 climbs and 2 down
7th Feb 2021, 11:04 AM
A͢J
A͢J - avatar
0
The solution is correct though I Am AJ ! I just don't fully get the logic behind it
7th Feb 2021, 11:08 AM
SuperKitsune94
SuperKitsune94 - avatar
0
Hey I Am AJ ! First and foremost thanks alot for helping me writing cleaner code. I'm quite a newbie so bear with me. What is the "new variable logic" you're talking about? Also you have that "=2" at the end like me. My question was that how is it possible that it can loop after a " break" ? Also what makes it stop at the right time so that the snail doesn't drop back down too much?
7th Feb 2021, 11:33 AM
SuperKitsune94
SuperKitsune94 - avatar
0
I Am AJ ! Yes, I understood but that "d -=2" what does it stand for? It's not simply "d-2" right? "d-=2" is domething you would write in a loop...that's why I'm confused because the loop is already broken..
7th Feb 2021, 11:53 AM
SuperKitsune94
SuperKitsune94 - avatar
0
Thanks alot but I'm probably missing somthing very simple but why does "d" have to be "d-2" at the end ? I know it has to do with slipping down again but why does "d" have to to be in a permanent negative state at the end? That would mean the snail would be in a permanent state of slipping down??
7th Feb 2021, 12:18 PM
SuperKitsune94
SuperKitsune94 - avatar
0
Can't you just tell me now? because I'm getting frustrated by it
7th Feb 2021, 12:27 PM
SuperKitsune94
SuperKitsune94 - avatar
0
I code to check +7 first, if it's still < 7 then -2. Otherwise, it continues +7 then check again . Add count start from 0 to count how many looping
8th Feb 2021, 10:56 AM
Ratna13
Ratna13 - avatar
0
Ratna13 ♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ Yes but why does the distance have to be -2 permanently at the end after the break? I stil don't quite see the logic in it?
8th Feb 2021, 11:25 AM
SuperKitsune94
SuperKitsune94 - avatar
0
I used for, inside for i used if. If +7 >= depth then count loop and break. It continues -2 if i+7 hasn't reach the distance. Why -2 because because it's rule. Snails must reach distance as soon as possible but snails in a day only have strength to reach 7.
8th Feb 2021, 11:35 AM
Ratna13
Ratna13 - avatar
0
No, if +7 has reached distance or more it breaks
8th Feb 2021, 11:36 AM
Ratna13
Ratna13 - avatar
0
So the key in +7. If +7 hasn't reached distance, poor snails, they have to go down -2.
8th Feb 2021, 11:37 AM
Ratna13
Ratna13 - avatar
0
Ratna13 thanks for your anwser but they're taking multiple days to reach the top so why do they only start to drop down after a few days? Because it takes them more than one day to reach the top, no?
8th Feb 2021, 12:01 PM
SuperKitsune94
SuperKitsune94 - avatar
0
Because the body of snails couldn't handle it therefore at the night decreases -2. Imagine if at the depth/or distance, it's snails's home. Then, when they reached home they don't need go down anymore.
8th Feb 2021, 12:04 PM
Ratna13
Ratna13 - avatar
0
And the mission completed
8th Feb 2021, 12:05 PM
Ratna13
Ratna13 - avatar
0
Look means respeating a path again and again In coding we you use for loop by setting a range Ex:- For(I=0;I<=7;I++) {Printf("%d",I) } Ans:- 0 1 2 3 4 5 6 7 This loop runs 8 times to print all the values of I.
9th Feb 2021, 9:33 AM
Balram
Balram - avatar
0
balram gupta thanks but this does not anwser my question about -2
9th Feb 2021, 9:36 AM
SuperKitsune94
SuperKitsune94 - avatar