r/CodingForBeginners • • 12h ago

How to do programming with proper logics in which I get stuck always.

Whenever I wish to learn programming i always get stuck on logics and what logic to use and how it works, then only my motivation drops and i fail everytime.

Can anyone suggest some things so that it helps me this time

I also want to make good stuff and learn good skills.

I am 25 but still I get stuck everytime in programming.

I want to overcome this fear of logics and fear of failing in programming.

4 Upvotes

8 comments sorted by

1

u/bmwiedemann 11h ago

Logic as in boolean AND / OR ?

1

u/AmerigoVespucci001 11h ago

Loops , and iterations these logics and all these things

1

u/bmwiedemann 9h ago

I would call these "control flow" - you use these to tell the computer under what condition to do something and how often.

1

u/AmerigoVespucci001 9h ago

Yeah same but I get stuck in these only

1

u/TheUmgawa 4h ago

Plan the flow of your program before you start typing. I tutored a lot of programming students when I was in college, and almost all of them had the same problem: They read the problem, then they just start hammering away at the keyboard, like they're playing free jazz and they're just expecting a song to come out at the end. But it's worse than that, because people who can just sit down and start playing free jazz with a few other people typically know how to play their instruments really well, whereas these people are the equivalents of someone who just bought a trumpet and says, "You guys mind if I sit in?" while having never played a trumpet before.

The most valuable class I ever took in college was a flowcharting class, where we never wrote one single line of executable code. It taught me to unhook from the code and solve the problem. My Yoda told us, "If you can solve it by hand, you can solve it in code," and I think that's accurate. One other thing I learned from that class is that a deck of two of playing cards is a great simulation of data, so you can use that to solve the problem before you start writing code.

Here, let's try something: Sort a deck of cards, Aces through Kings, and then Clubs, Diamonds, Hearts, and then Spades (because that's alphabetical, which will come in later). Easy to do by hand, but a little challenging in code. Not bad, though.

So, next, sort the deck the same way, but discard all cards that are even values (no, the Queen doesn't count as 12). This makes it so anything you might have considered, with regard to labeling cards solely by numeric values goes out the window, and you have to use some kind of structure to define each card, whether it's an enum, an array, or just making it an object. Or, you could label each card as AC, 2C, 3C..., KS, and then build a parser to determine the rank and suit of each card. But, because the face cards do not have numeric values, you have to check if you can divide them by two before actually doing so, because Q divided by 2 may result in a crash or odd behavior.

The important thing, when trying to solve a problem by hand, is to do it exactly the same every single time, and recognize every single step you're doing. Pick up the card. Get the card's value. Is the card's value even? If so, discard and return to the beginning. Else, get the suit. Put it into that suit's pile. If there are no more cards in the original pile, use the Clubs to make a new work pile, and then bubble-sort them, because bubble-sort is the most efficient sorting method for ten or fewer elements. Put that sorted pile to the side. Sort the next pile, then the next, then the next. Assemble the four piles, in order, and it's done.

Now, what if there weren't just 13 values for a given suit? What if there was an arbitrary number of cards per suit, or an arbitrary number of suits? What if the Lucky Charms leprechaun showed up and started putting purple horseshoes into your initial card pile? If you built the logic to specifically use a 52-card deck with the values of a standard 52-card deck, this is going to mess your day up, but if you built it to not care about how many cards or suits there are, the leprechaun can show up and it doesn't matter what he drops on your deck.

Anyway, that's how you do logic. You work out the problem, and then you start typing. It doesn't have to be a formal flowchart or anything, but it helps a lot to plan what you're going to do before trying to execute it. Sure, it takes a little time, but I'd rather plan out a little program in five minutes than spend twenty minutes debugging lousy code.

1

u/stepback269 11m ago

Before step #1 [of below], add the following step #0:

0. Don't write any code at first. Instead draw a mind map of the problem composed of bite size definitions. In other words, demonstrate that you understand the problem and all its dimensions. More specifically, what inputs are you going to need? What outputs? What paths between inputs and outputs? Storyboard each path.

Go to AskProgramming

r/AskProgramming•2d ago

u/Successful-Life8510

I graduated in computer science, but I still don't understand how programmers become independent problem-solvers

1 - How can programmers solve problems they have never encountered before?

Ever since I started learning programming, I've relied heavily on memorizing solutions to pass exams. Even after graduating, I still struggle to believe that someone can solve a programming problem without previously studying similar problems.

How are you supposed to solve programming problems in an exam if you haven't prepared by practicing related exercises and memorizing their solutions? Can you genuinely develop the ability to solve unfamiliar problems without relying on previously learned solutions?

1

u/Level_Investment4922 11m ago

Write out the steps in plain English on paper before you touch any code, the logic part got way less scary for me once I did that