0

First question so im gonna make it a general one

What i'm trying to say, is is it ok to "lean" or have a "crutch" when it comes to coding? Like im in an intro to programming class, and I avoid using while and dowhile loops, anytime a loop is needed I either try to find a solution that doesn't involve loops, or I use a for loop.

DO i have to go out of my way to learn these loops or will I figure them out as I get better at coding, and so on for the other things I avoid doing/depend on only one strategy

  • 1
    In this specific case, no, it's not a good idea, something like `for-next`, `do-while`, `while-do` are all foundation concepts which are pretty much present in any language – MadProgrammer Oct 26 '15 at 03:35
  • Use the right construct for the job. For loops are great, but there are some things you should really do with a while loop. You'll understand when to use what as you gain more experience. – wadda_wadda Oct 26 '15 at 03:36
  • What will you do when a time comes when you have to inspect/understand someone else's code and he/she prefers using something that you ignored. Initially if you want to ignore you may but try learning different things as they may be helpful at some point of time. – Laschet Jain Oct 26 '15 at 03:36
  • There's a reason the analogy is a crutch: while it can help you get to where you need to go right this moment, the fact that you need it means you're not functioning at 100%. Especially when you're in a class, where the whole point is to learn, you should really try to get away from relying on crutches as much as possible. – yshavit Oct 26 '15 at 03:48

2 Answers2

1

Loops are one of the fundamental tools we as programmers have to use. The for-loop and the while-loop are especially useful ones to know. I would say you should "go out of your way" to learn them now because they are essential to becoming a good programmer and they are not that much different than a for-loop. If you want to become good at coding, you have to know the different tools you have available to you.

Here's a quick lesson:

Use a while loop for something you want to repeat until a condition is no longer true. It is similar to a for-loop except for the fact that you don't have a counting variable. A do-while loop does one loop before checking the condition.

For a more in-depth answer on the difference between the loops, I would encourage you to check out https://stackoverflow.com/a/2950945/3345085 (Although it's an answer to a C question, it is essentially the same.)

Community
  • 1
  • 1
Logan
  • 1,575
  • 1
  • 17
  • 26
1

In this context, it is not recommended to avoid using loops. They are fundamentals of a programming language. If you don't use them, your code will easily get messy and hard for others to read. do...while, for while are equally important. You can't say, "I just want to learn while loops but not the others". Because you eventually need to read others' code and when you see a do...while loop you can't just ignore it, can you?

Conclusion: You can use only one kind of loop in your program (NOT RECOMMENDED). It is possible. But you still have to learn them because they will appear in others' code.

However, in programming, there are some other things that you can have preferences. If you don't care about other factors, you can say, "I like using insertion sort to sort arrays". You can just use insertion sort. I mean this kind of stuff is personal preferences but loops? Just no.

Let me just sum up my points here:

In this special case, don't use only one kind of loop and learn ALL of them. However, in other areas, you can have your own preferences, such as sorting arrays and reading files and design patterns (if you don't care about other factors)

Note: Actually as a software developer, you have to keep learning. If you don't understand something, learn it! Look at the experience of NikG below

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • As a software developer, one the biggest goals you should have is to keep learning, even after you're done with schooling. I remember at my first job I had to learn C#, and I avoided anything Linq-related like the plague. Now, after being exposed to it and seeing how it works, I can see how it makes so many problems vastly simpler, and I can barely remember how I managed to get by without it. Keep learning - it's tough now, but the investment of time gives a great payout! – Mage Xy Oct 26 '15 at 05:28
  • @NikG Yeah you're right. I should add that to my answer. – Sweeper Oct 26 '15 at 05:30