Loop and break | Sololearn: Learn to code for FREE!

+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;} }

2/7/2021 11:00:18 AM

SuperKitsune94

24 Answers

New Answer

+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; } }

+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.

+3

SuperKitsune94 Take rest and give some time and think again why d -= 2 should be there even after break loop.

+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

+2

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

+1

SuperKitsune94 https://code.sololearn.com/Wy5Cgc1k45vy/?ref=app For any queries please concern me

0

The solution is correct though I Am AJ ! I just don't fully get the logic behind it

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?

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..

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??

0

Can't you just tell me now? because I'm getting frustrated by it

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

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?

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.

0

No, if +7 has reached distance or more it breaks

0

So the key in +7. If +7 hasn't reached distance, poor snails, they have to go down -2.

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?

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.

0

And the mission completed

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.