Compare and contrast the WHILE loop and the FOR loop.
1. Constrast: For Loops allow you to run through the loop when you know how many times you'd like it to run through the problem such as
for (var i; i < 10; i++);this will continually increase i untill that condition returns false, any number can replace the 10 even a variable. but it will quit once the condition is no longer being met. This is best used again for loops that you know how when they should stop.
While Loops allow you a little more flexability in what you put in it, and when it will stop such as
while ( i < 10) you can also substitue in a boolean(true/false) for 10 as well as many other types of varibles.
The key difference between the two is organization between them, if you were going to increase to 10 it'd be a lot cleaner and more readable to use a for statement, but on the other hand if you were to use an existing variable in your program in your loop parameters it'd be cleaner to just wright a while loop. In the For loop you MUST create a new variable, thats not true for the While loop.
Your discussion should identify the similarities, differences, advantages, and disadvantages of each structure.
WHILE loop: will always evaluate the condition first.
FOR loop: allows you to initiate a counter variable, a check condition, and a way to increment your counter all in one line.
Differences: The main difference between the
for's and the
while's is a matter of pragmatics: we usually use
for when there is a known number of iterations, and use
while constructs when the number of iterations in not known in advance. The
do ... while issue is also of pragmatics, the second executes the instructions once at start, and afterwards it behaves just like the simple while.
Advantages, and disadvantages:
Any for loop is equivalent to some while loop, so the language doesn't get any additional power by having the for statement.
For certain type of problem, a for loop can be easier to construct and easier to read than the corresponding while loop. The for statement makes a common type of while loop easier to write. It is a very good (perhaps the best) choice for counting loops.
Which data structure do you consider to be more robust? Why?
- NOt sure
Content will be erased after question is completed.