Exercises with loops

Yet another newbie hoping for a little bit of help.
Despite reading a lot of theory about for and while loops, i realised that all this theory doesn't help me with exercises. I struggle every single time i have to use loops, because i can not simply understand how to choose the correct loop.

For example this task:

It takes b minutes for mum to make the first dish. It takes k minutes to make the second dish. And it takes k minutes longer to make the third dish, compared to the second dish etc.. (So assuming b = 10 and k = 3, it takes 10 minutes for the first dish, 13 minutes for the second dish, 16 minutes for the third dish etc.. You get the idea ;)) Write a program, that would calculate, how many dishes will mum make before her kids comeback. Kids will come back after t minutes. All data is int type.
To check: If b = 17, k = 3, t = 65, mum would make 3 dishes.


At first glance, it's a pretty simple task. But i got stuck at the moment i had to use a loop. I tried to create a for loop, later i tried to use while loop - no result.
The problem is, i don't know what condition shall i choose, what should the variable initialization be etc..
Simply said, i just write: for (int i =
And then i get stuck.
Any help, either with the given task, or with "picking the right loop overall" would be really appreciated, since this is such a simple thing that i can't get over.
Thanks in advance : )
You don't need a loop for this. This is a very simple linear equation.

Think of it as an algebra problem and not as a programming problem. Figure out the equation, then just write that equation in code.
Last edited on
Well maybe, but i want to learn loops.
That said, my biggest goal is not solving this exercise, it's being able to do well with loops.
Create the program with if statements. Then turn every if into a while loop with a return in it, so that you won't have an infinite loop.
Last edited on
Right tool for the right job. Trying to learn how to use loops by applying them to a problem that doesn't require a loop is like trying to learn how to use a frying pan by driving in a nail with one.

It will be much easier to apply the concept of a loop to a problem which actually uses one. If you're really interested in learning loops, I'd recommend finding a different problem to solve.

Here are two on this page:
http://www.cplusplus.com/forum/articles/12974/

See "while( user == gullible )" and "pancake glutton".
Topic archived. No new replies allowed.