# In Programming, How do people think? (especially in loops)

I know I can discover this answer myself from experience, but I think it is not a bad idea to hear others thought.

as introductory, I actually very interested anything involving logical understanding(math, physic) which then, brought me to programming, but then I discover that its(programming) more of an `organizing data(breaking big problem into smaller one) and creativity` subject, maybe I'm wrong?

it's such a shame that I can't solve a problem in chapter 1, from C++ Primer, 4th edition(Exercise 1.24). I think Summary for the exercise could give idea of what make me think of `creativity` has strong relation with programming:-

``````- the program want to read several transaction(which contain "different" ISBN)
-> which make me think of (while >> cin) loops.

- the program want that one with "same" ISBN, to sum up(which I thought they want
me to use "a = a + b" equation)

- then, print the record on the screen, based on ISBN,(where same isbn was

- test input:-
(ISBN) (no. of book) (price)
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00
0-201-78342-X 5 26.00
0-201-78341-X 1 35.00
0-201-78342-X 5 55.00

- EXPECTED result:-'
0-201-78345-X 5 22.50
0-201-78342-X 10 40.50
0-201-78341-X 1 35.00

-most important part is, the author want the reader to use his written class
`Sales_item` type , because of that, the operator and 'tools' is some sort limited

-where the operator is:-
> arithmetic(+, -), I/O(<< and >>), assign(=), and "same_isbn" function, where the
function is bool type indicating between 2 arguments if the ISBN is same. ONLY.
> of course you can use the built in library and some other usual noob
stuff(<iostream>, etc...)
``````

and I don't know how to think anymore, already draw a lot of "loop diagram" on how the program works, but can't help with such tools so limited which I can't do much(stuck at loops, because if I edit a little, it affect whole program)

well, it's hard to explain of what I want to ask, so I think maybe question above can help. back to the title!

so, I already learned vector(already passed to chapter 6 in Accelerated C++), and I find some sort of solution using the container, so I came to conclusion that author want to test reader's creativity in programming from that exercise(above) or what?

Then, There come in my thought what programmer will do if they got limited tools for what their language have and their task(which why programming language is specified for certain task, right?) does programming actually involve that much creativity?

and. if possible, for experienced programmer, which is already used to loops, how do you think on how to control it ? I find it very frustrating when trying to edit them(after analyse it I can understand how it works, but then, I'm just depend on trial-and-error method to get my desired result(which I bet the "wrong" way on how programmer should think)

P.S. if you can answer the question, give some hint, if not, just don't bother, get to the main question(title)

-
There's a basic rule of thinking that's called the "Zero-One-Many Rule": You either have zero or one of something, in which case you might use a conditional to distinguish the two, or you may have many of something (including zero), in which case you use containers and loops. That is, you should never explicitly have two or three related objects; they should immediately go into a container. Containers and loops go hand-in-hand. – Kerrek SB Dec 16 '11 at 14:20
thnx for the link, I'm always google before asking for something on specific website, but maybe I'm unlucky enough to not find the result or I need to upgrade my googling's skill... :P – Vastor Dec 16 '11 at 14:31
Simple, make short sentences. You can transform this skill to programming, too. Split big problems into single commands and you will get a solution. The problem with your writing is not the grammar but the never ending sentences combined with stuff inside brackets. Write simple and keep it simple. – WarrenFaith Dec 16 '11 at 15:07
... and then, re-read and remove everything not relevant. Stating a question (or anything) concisely takes more work, but you're likely to get more and better answers. You may even learn something just by editing it. – Useless Dec 16 '11 at 15:45

## migrated from stackoverflow.comDec 16 '11 at 14:14

You're overthinking everything though - just say it in plain English.

"I want to let the user type in items, repeatedly, until they are done." or "I want to read entries in from a file until the file is empty".

In the first case, you can do something simplistic, let the user loop until they are done entering data, and give them a sentinel answer to type in that can let you break the loop.

``````cout << enter some data, or x if you're done;

{
map["ISBN"] += value;
}
``````

By the way, I used std::map because you can use the ISBN as a key, and it will allow you to always find that key if it exists (or insert it if it doesn't) and increment the value associated with it all in that one line - try googling it, it's a std:: container much more suitable than std::vector for this purpose.

Sorry I can't write more, at work :P

-
 well, I'm not learning map yet, so yeah, your trial to answer the question is not valid,:P but stil if you don't mind, answer the main question? :) p.s I'm not native-speaker, so mind my language skill, I try my best so that people would not confuse in my word, which I felt that I fail, again?! :( – Vastor Dec 16 '11 at 14:32 btw, does overthinking that bad? :S, btw, I think my overthinking is coming from the question in Accelerated C++(which a reason that I put it off in chapter 6, then goes to above's book) well, atleast I'm relieved now that I'm not "under-thinking", lol – Vastor Dec 16 '11 at 14:56

If English is not your first language, good for you for making the effort. English is not easy.

At the same time, I'm very confused by your question.

Can I make a suggestion - that you pop up a level in your thinking? Programming is not really about following a book and doing exercises about loops or other control structures. Those are just means to an end.

Programming is about having a problem that you want to solve, and using a computer program to solve it. So the first thing is - you should identify such a problem. It should be simple enough that you can do it. There's no need to set yourself up for disappointment.

Then think in general terms of how the program will be laid out. What is its input and output? Does it need to save information between times that you use it? Given its input and/or saved information, how would its output be determined / calculated?

From there, start programming. I suggest you take baby steps. Write a small program to do part of the job, to get some experience with it. Then set that aside, and start over with another small program to do a different part of the job. After you've done three or four little programs, you can start to write the overall program. Even so, you might decide that you need to start over yet again. That's OK.

Since you are working on a program with a purpose, you will be able to see how the things in the books might help you in that purpose. Without purpose, you can't expect these things to make any sense.

If I can make an analogy that I hope doesn't bother you too much, a book full of programming constructs is like a box of Christmas ornaments. The problem you are trying to solve is like the tree. Without the tree, you have nothing to hang the ornaments on. They just stay in the box, useless. With the tree, you can find the best place for each ornament, and be happy with the result. A well-built program is like that.

-
 thnx, for so much tip :D, especially the part "save information between times that you use it?" and the christmas tree analogy, even though I never build one. btw, I can't see much of the 'tree' when the objective of the exercise is so vague, :) – Vastor Dec 16 '11 at 23:57 @Vastor: When I taught programming, I got the students to where they could use basic techniques (in BASIC) and then I had them start projects. Suggested ideas were: some kind of banking or investing, some kind of simple game like Wumpus or Hangman, something from science like genetics, role-playing or adventure game, sports, like baseball or football, sailing. So I could be specific, but if they got their own idea that was even better. So they would say "How do I accomplish XYZ?" and then realize "Oh that's what that language thing is for!" – Mike Dunlavey Dec 17 '11 at 4:33

The essential words for loops are

"while", "until", "for each", "for all", "every", "all".

When you use one of those words, it probably means a loopy semantic is indicated.

A loop has several elements:

• initial condition (what does the program state have when it starts the loop)
• exit condition (what is the condition that will be true to stop looping)
• loop body (what happens inside the loop)

Further, most loops have some sort of step condition - either the next element in the container is gotten, or the next index into the arry is determined.

Couple examples:

Print all integers from 1 to 10.

``````for(int i = 1; i <= 10; i++)
{
cout << i << endl;
}
``````

Loop through each element in a vector and print it

``````for(int i = 0; i < vec.length(); i++)
{
cout << vec[i] << endl;
}
``````

Take input and process it until the user types "end"

``````bool go = true;
while(go)
{
string s;
cin >> s;
if (s == "end")
{
go = false;

}
else
{
process_input(s);  //some function defined elsewhere.
}
}
``````
-
 thnx, for trying to answer. i actually have no problem to deal with the loops in current and next 'step', but, I'm having problem dealing with the 'step' before, which if the `i = 4` (in middle of loops) where this part messed up the desired result the loops before `i = 3 or 2 etc...`, hope you understands – Vastor Dec 16 '11 at 23:46 where it is more like storing information by using loop problem, which is why I'm mentioning vector, which is much easier to "navigate" all through the loops... – Vastor Dec 16 '11 at 23:59 @Vastro: One of the ways to understand a programming task is to chop it up and do it bit by bit. Throw out the things you don't understand and build the things you do. It is a well-known fact that programming requires hands-on work. – Paul Nathan Dec 17 '11 at 0:41

The way I think about loops is the way I learned from reading Accelerated C++, which has helped me a lot (the skill is transferrable to other languages as well, of course).

Whenever you write a loop, think of a loop invariant - that is, a statement that is true whenever you are about to iterate over the while loop again. For a simple example, if I have a vector that holds a bunch of strings and I want to print them out line by line, I would have:

``````i = 0;
// invariant: I have printed out 'i' lines so far
while (i != myVector.size()) {
cout << myVector[i] << endl;
++i;
}
``````

The invariant should be designed so that upon termination, it is clear what the value of the variable in question is (in this case when the loop terminates, we know we have printed out `i = myVector.size()` lines, which is good).

Second, to ensure the invariant is true all the time, make sure it holds true right before you enter the loop, and when you are at the end of the loop. Before we enter the loop, `i = 0`, meaning we have written 0 lines so far. Good. The first line inside the loop, we write a line to the standard output stream, making the invariant false. Therefore, we include a `++i` to increment `i` to make the invariant true again at the end of the loop.

-
 thnx for answer, even though not really accurate of the answer that I desired, and it's me to be blame, I really need to fix up my english skill =X .. btw, maybe you can refer to my comment on Paul Nathan answer for figure out what I'm not understand... – Vastor Dec 17 '11 at 0:09