The easy way to think of switch statements is alot like glorified if/else statements. I personally agree with MatthewRock, I found pointers and references more confusing, but I have a background with basic which used similar loops so I had no problem with those. Imagine instead of using if/else to decide how to respond to a variable being equal to 1,2, or 3, we use a switch statement. If we did, it would look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int c=7;
switch (c)
{
case 1:
cout<<"c is 1";
break;
case 2:
cout<<"c is 2";
break;
case 3:
cout<<"c is 3";
break;
default:
cout<<"c is not 1,2, or 3";
break;
}
|
It's a real simple example, but it's the exact same as this:
1 2 3 4 5 6 7 8 9
|
int c=7;
if (c==1)
cout<<"c is 1";
else if (c==2)
cout<<"c is 2";
else if (c==3)
cout<<"c is 3";
else
cout<<"c is not 1,2, or 3";
|
The reason for all those break statements is that they define the end of each case, or if statement, if you prefer. If you were to leave out the break on line 10, for instance, it would say c is 2 followed by c is 3. This can be annoying if you forget your break statement, but it can be useful when used strategically. For instance, if c is 1, you can have it do all things, if c is 2 it can do all things except the first, etc. But in this case, both code results in the same thing. I know that sometimes it is difficult to wrap your head around a new concept, and I think presenting it as an alternative to something familiar helps so you can see the concept at work. If only some of the learning materials took a similar approach, I would have a better understanding of pointers, references, and vectors, so I share your frustration. We all hit obstacles and snags in the learning experience.
Try not to get too discouraged; sometimes the right example or code snippet comes along and suddenly you understand how a concept works, although you may be more comfortable using it a certain way, at first. It's all part of the learning experience. If you find for loops particular confusing, stick with while loops where you can control the loop directly, it may be different, but has the same ability as a for loop, it's only that the counter is not "built in". Don't be afraid to try other people's code with loops in them, and changing the values to see how it affects program behavior. Try to stick with simple stuff, like outputting a few stars to each line, until you get the hang of it. If you start another thread about loops, or post in another loop related thread, I'm sure you can get plenty of guidance on the topic, and the same can be said for functions and arrays. I myself had trouble with arrays, at first, but then I had a major breakthrough and managed to create a tic tac toe game using one dimensional arrays, and from there changed it to a 2 dimensional array and then created Connect Four. But we all experience confusion when first encountering unfamiliar concepts; try not to let it hinder you too much, the key is in finding simple examples that aid in understanding how something works.