I recently received an email asking whether I've ever faced programmer's block. The emailer was referring to those times you sit and stare at a problem but never make any progress. You feel stuck and, in some cases, just give up. How does one get past that?
The good news is that I think all programmer's face this feeling at one time or another, and most likely, they feel it rather often. The thing that changes with experience is usually the level of complexity of the problems causing stuckness, not whether a programmer will get stuck in the first place.
That might seem really depressing, especially if you hear this after your first extremely frustrating moment of stuckness. But it's not all bad news. As you learn more strategies for getting unstuck, you don't stay stuck nearly as long.
Some strategies: learn to experiment. When you first start programming, many tend to be a bit afraid of tinkering with code, trying different things with an eye toward understanding better what's going on. It's not just trial and error; you have to be strategic. You have to take the time to understand why something finally works, even if you hit upon the answer randomly.
To improve your experimentation skills, you need to learn to debug. Whether you just print the values of variables at key locations or use a fully-featured graphical debugger, learn how to display as much as you can about your code so you can ensure your mental model of it is correct. You must learn techniques that help you first narrow down where the problem is, then you can tackle figuring out what the problem is. For example, you might narrow down that a problem appears inside a loop. From there, you can start printing out the variables you are changing in the loop to see if they are what you expect.
As you learn more and more about programming, algorithms, data structures, and so on, your toolbox of problem solving techniques grows and grows. Things that seemed so hard in the beginning are now no problem to spot, all thanks to experience. Of course, new problems are introduced, but you know how to tackle them, thanks to your ability to experiment and debug. It takes time, but damn, it feels good when you finally get unstuck again!
If you'd like to read about some more specific programming problem solving techniques, check out Think Like a Programmer, which I've reviewed here. (I wasn't compensated to say this - I just really like the book!)