I want to get my M.S. in C.S., but I'm struggling in undergrad?

So, I started my undergrad with a bad semester. I “failed” my introductory programming course with a C- (you need a C to be considered “passing”) and managed a C in another linear algebra course my first semester, having a 2.7 that semester. I managed to have semester GPA’s well above 3.0 the next three semesters (particularly the 2 after that, where they were 3.7-3.8) and I became more and more confident.

Yet, this semester, I’m going back to having another 2.7-2.8 GPA. I messed up from the start when I had to withdraw from an important course (considered the hardest required course of my major) because it was just stressing me out and I’m pretty sure I had overloaded on this semester, this gave me some depression and anxiety that cascaded into my other courses. I fell just short of passing my Theory class with a C- and will likely get a C/C+ in my Algorithms class.

The thing is, I wanted to go to another school to pursue my masters. My GPA was previously at a 3.4, but now it’s going to fall to a 3.3. Plus, now there are two classes from this semester that I have to retake the next semester, if I can. I’m just so worried about what is going to happen to my graduate school aspirations now and what future semesters could hold for me. Any advice moving forward?

TLDR: Messed up again after a rough semester two years ago, advice?

Let’s get this question answered first, regardless of your GPA: “Why do you want/need to go to grad school?”

^Yes, the above question is very important. In addition, it appears that you are struggling in your computer science classes specifically. Do you know why you are struggling? Do you have a hard time understanding the material, or are you not getting in enough time studying, or are there other things interfering with your schedule? You’re pressing forward, but you need to pause for a moment and try to figure out why your performance in CS classes is so spotty - especially before you try to pursue an MS.