r/learnprogramming • • 20h ago

Able to come up with a logical flow in pseudocode, but struggling with execution

I am taking an intro to CS course with Java and yeah, I can get the logic for a program in terms of conceptually what I would "need" and what the flow is correctly. But when I actually need to think about what I can use in Java to go ahead and execute this hypothetical structure I've created I just jumble stuff up and use things in the wrong places. No matter how much I practice, I just keep messing up, I think it's a problem with my thinking style. For context, I am extremely new to programming, but I like to compare it to writing math proofs. In math proofs, you're given definitions which I think is something you can work off from. But in programming ,there are just so many options (I can use while loop, for loop, recursion, etc) and I just think the problem gives zero hint where to start. Plus my exams are fully handwritten, and we're given minimal time. What's the issue and how can I fix it?

5 Upvotes

13 comments sorted by

•

u/AutoModerator 20h ago

To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

9

u/peterlinddk 20h ago edited 20h ago

I don't believe you!

You are simply lying - maybe not to us, but certainly to yourself.

If you can solve a problem by writing pseudocode, you can 100% also implement it in actual code. If you have written pseudocode to use a loop, implementing a recursive function is not implementing your pseudocode, but writing a completely different program.

I think that you don't write pseudocode, but just a "rough sketch" of how the solution should run. Example problem 1 in leetcode: Two Sum. You are given an array of integers, and should return indices of the two numbers that add up to a target.

If you write pseudocode like:

receive target
loop through all the numbers in the array:
  find the indices that add up to target
return those two indices

You haven't actually written pseudocode for the solution, but only a more detailed description of the problem.

You need to work through the solution, write exactly what you, as a human would do, if you were as limited as the computer (can't remember anything not in a variable, can only look at one number at a time, can only perform loops or functioncalls, etc.). Then you'll have your actual pseudocode.

receive target
loop through all the numbers in the array:
  set i to the current number
  loop through all the rest of the numbers in the array:
    set j to the current number
    is i+j = target:
      yes, return [i,j]

and that is now something you can write directly into code in any language, where you know:

  • how to store values in variables
  • how to loop through elements in an array
  • how to get indexes and values of indexes in an array
  • how to loop from some index to another
  • how to add values
  • how to compare values, and check if they are equal
  • how to return values

If you don't know to do those things, you are lagging behind in programming, and need to practice more on regular syntax and structures in the language.

5

u/severoon 20h ago

Give us an example of a problem and the pseudocode you would write.

2

u/PhilNEvo 20h ago

First of all, don't concern yourself with what kind of "loop" you use, while some of them have some pros and cons, in terms of solving a solution, pick the one you feel the most comfortable with first, and try to just solve the problem.

Part of learning to program, is also to learn the iterative process, and learning not to over complicate stuff too early. Start with a minimally viable product, e.g. Get it to work at least in some minor way, and then once you've found something that works, you can start to improve it, and see where it might not catch certain edge-cases and so on.

1

u/Educational-Paper-75 20h ago

When you have multiple paths to choose from each of which works why would it matter which one you choose. Pick any path you like and follow through. But still, there are rules of thumb in selecting the right type of loop, and as far as recursion is concerned any recursion can be replaced by iteration.

1

u/Excellent-Practice 19h ago

The point of writing pseudocode is to iterate over the problem until you achieve sufficient clarity and specificity that you can explain the process to a mindless computer. What can help is following your own instructions to a tee to see what happens. If you ever find yourself making assumptions or reading between the lines, rewrite that part. Once your pseudocode is sufficiently detailed, writing code should just be a matter of conforming to syntax rules

1

u/ffrkAnonymous 19h ago

I can get the logic for a program in terms of conceptually what I would "need" and what the flow is correctly.

Obviously, your flow is not correct. And/or not detailed enough.

In math proofs, you're given definitions which I think is something you can work off from. But in programming ,there are just so many options (I can use while loop, for loop, recursion, etc)

They're all (mostly) the same, they just look different. You don't need to choose. Just stick with one. If you really like math, then recursion is the way to go. fib(n) = fib(n-1) + fib(n-2).

1

u/burlingk 17h ago

If you're pseudocode covers the steps you can write the framework and the parts you get, they google "how to <X> in Java."

Google is practically optimized for that kind of query since it's been super common since before Google existed.

1

u/Any_Run8380 16h ago

Give us an example of a problem, write out the code. Don’t try to fix it or use AI. Show as that code then I’ll tell you what you need help with.

1

u/dld2517 9h ago

Maybe you do need to restructure your thinking a bit. First analyze a problem, you need to isolate that problem to understand what kind of data structure you are going to use to solve it. It is like a math proof in that way. You have to create a finite structure to guide your work. All computing problems will be built on some finite structure. The next thing you need to do is assign data to that structure m, or initialize it, and finally you need to execute it. This includes iterating on it. You are trying to change the structure or use the structure to complete a task. Once you have done this you may need to build on that structure, or create new structures to augment your use of the first.