Some thoughts I sent to a student from my church's youth group:I was thinking about the question you asked: Can you ask God to forgive you before you do something wrong?
One way to define sin is: Anything that puts distance between us and God. In other words, sin messes up our relationship with God. This is how it happens: We feel ashamed for something we've done or thought, and we want to hide it from God. Plus, we afraid he'll be angry—that's another reason we try to hide from him. The problem is, when we "hide," only we see it as hiding. We're not actually hidden—God already knows what we've done. When we're trying to hide, we're not talking to God. (Like how you might avoid your mom if you've done something wrong.) That lack of communication puts distance between us and God.
If you ask God to forgive you before you do something wrong, that shows you really don't want to mess up your relationship with God. It means that you love God, and you're concerned about your relationship with him. Still, if you go ahead and disobey God, your actions aren't matching up with what's in your heart.
Here's an example: Imagine that your mom tells you to do your homework before watching TV. You want to obey your mom because you love her. But there's also a TV show on that you really want to watch, and your homework isn't done yet. If you go ahead and watch TV before doing your homework, does that mean you don't love your mom? Of course not. Still, it's disrespectful to break her rule. And, even though you do love her, you didn't show it when you had the choice of obeying or disobeying.
Your actions (watching TV before doing your homework) aren't matching up with what's in your heart (you love your mom and want to obey her).
This is something most—if not all—Christians struggle with. I do. I love God, and I don't want to do things that are disobedient and disrespectful. But sometimes, I do them anyway.
Here's a prayer that might be helpful to you: "Dear God, you know what's in my heart. You know that I really want to obey you, because I do love you and respect you. Help me to match my actions with what's in my heart. When I want to do something that's disrespectful to you, please remind me how much you love me. Remind me of all the great things you've done for me. I don't ever want to put distance between us—I want us to stay close and connected, and I know that means talking with you. Thanks for always listening to me."