|
-
Confusedz
Hey guys,
Im a freshman in high school taking c++ and all we're currently doing is simple dos programs but now we just started the loops chapter and I understand the loops and every thing fine but I dont understand when and where loops should be used in programs. Could some one write a simple program showing me why I should use a loop instead of other means of accomplishing the goal? Oh yea, we have to accomplish the goal with loops because this is the loops chapter;-). This isnt a homework assignment either but it's me being eager to learn this stuff.
Thx.
------------------
:-)
-
Ursus Arctos Moderatis
Learning C++ as a freshman in highschool? Rock on man! I didn't touch the stuff until I was a freshman in college, I wish I had an earlier start on things. I'm happy to see your interest in programming so early on, stick with it buddy.
Anyways...Looping structures are a very importat element to a programming language. No language can really be useful without them. Loops are used for just about everything...let me try and list a few scenarios:
1) Have you gotten to arrays yet? You'll often find yourself looping through an array in order to search for an element in that array, or possibly even to sort the array.
2) Consider a computer-based cash register. These are simple transaction programs which are actually running through almost-infinite loops. These loops only terminiate when someone enters a defined Quit Action, such as pressing "q" or typing "quit". So programs such as these might have a loop who's logic is similiar to:
while( InputValue != 'q' ){
scan product...
process transaction...
accept payment...
print reciept...
}
Thus the program will accept transactions all day long, over and over again, 1000 times, even 10,000 times until someone presses "q"
3) Suppose you want someone to enter payroll data for 100 employees. The easiest way to do this would be to place the cin>> process into a for loop which runs 100 times, instead of writing the cin>> routine 100 times yourself.
Give me week with a keyboard and I could probably make a list of 1001 different ways looping structures are useful in programming. The list goes on and on...they're very useful, and very necessary.
-
Mako Shark
looping strctures can be used to repeatedly execute some code until a certain condition is met. this is useful because say if you need to do some task 10 times, you do not need to coy and paste your code ten times, also if the number of repeated execution is determined at run time, there's no way you can predict how many times to copy and paste your code. an example of this would be the input of some data from a text file where each line is considered one entry. if the file contains 100 lines, it would be absurd to write 100 calls to whatever code that retrieves a line, a loop would be useful here.
------------------
Keep it brief
-
Thx a lot guys..I'm getting there now, I guess I'll just have to keep do examples out of my book...
------------------
:-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|