C++ is challenging, and 7 weeks part time study is next to nothing; even full time study its not much unless you are coming into it for language only (meaning you know a lot about programming, but not about this language, in which case you are just learning syntax without concepts mostly).
Programming is challenging too, so learning both at once is a lot of work.
Loops are almost never well presented to students.
so, a short extra for you:
all C loop types can be used interchangeably. Trust me on this for a moment. The range-based for loop (RBFL) is special, it represents a pointer based loop in C as a cleaner syntax using c++ iterators (pointer like things) and you can't make a while loop do that (without going back to C syntax) easily.
So ignoring the RBFL for today...
we have while, do-while, and for loops in C++ (also ignoring go-to).
if they can all be used interchangeably, how, and how do you decide which to use?
well, its not too hard :)
the for loop is used when you want to have one or more variable activities or actions in the loop body. The for loop syntax, of course, is for(initialize, loop stop condition, action). You can be obnoxious and make the action do anything, like cout: for(int i{}; i < 10; cout<< ++i << endl); //see the ; on the end of the loop? all the fun is in the action section!
This is showing off, though, and the recommended practice for most code, just so it is easier to read, follow, work on, debug, and so on is to use the action to modify the variable(s) in the initializer part. so more typically you see for(int i{}; i < 10; i++) {actions;}.
a for loop that is a while loop looks like for(; i < 10 ;). This is kinda stupid looking, and senseless: use a while loop already!
the while loop is used when the loop body may execute 0 or more times.
it is simply while(stop condition) and stop condition can be met the first time, which would skip the loop. A while loop can play at being a for loop:
1 2 3 4 5 6
|
int i{}
while (i< 10)
{
actions;
i++;
}
|
here again, that is sort of goofy: use a for loop!
and the do-while is the hardest for students to grasp.
the main point is simply that you want to run the loop body at least one time, regardless of the stop condition. This nets you that idea, and it also prevents the clunky setup outside the loop stuff you can see with a while loop. a quick example of that, lets say you want to validate some input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
//the while way:
prompt for input //you can also set input invalid here instead.
cin >> input; //but then you have odd logic if you have an invalid/try again message.
while(input is not good)
{
// repeat the above lines
prompt for input again
cin >> input; //again...
}
//and the do while way
do
{
prompt
cin >> input
//and simply if invalid print try again message here if desired.
} while(input is no good)
|
all that to reiterate (ha!) ... you can use any of these 3 for anything that needs looping. So if you pick the wrong one, its not the end of the world. But as you can see, if you pick the right one, it will be easier to write, easier to read, and more elegant than just clobbering it with whatever you felt like typing that day. So thats it in a nutshell.. the right loop saves clunk and makes it nicer both to write and for the reader. If you used the wrong one once in a while (ha!), its ok. It happens to everyone!