Compare and contrast the WHILE loop and the FOR loop.

Programming
Tutor: None Selected Time limit: 1 Day

  • Compare and contrast the WHILE loop and the FOR loop.
  • Your discussion should identify the similarities, differences, advantages, and disadvantages of each structure.
  • Which data structure do you consider to be more robust? Why?
  • Nov 10th, 2014

    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.

    http://www.codecademy.com/forum_questions/510e3c1a3011b8fa25005255

    2.Compare :

    http://www.peachpit.com/articles/article.aspx?p=1312792&seqNum=5 

    Your discussion should identify the similarities, differences, advantages, and disadvantages of each structure.

    WHILE loop: will always evaluate the condition first.

    from : http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming

    FOR loop: allows you to initiate a counter variable, a check condition, and a way to increment your counter all in one line.

    from: http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming

    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 while vs do ... while issue is also of pragmatics, the second executes the instructions once at start, and afterwards it behaves just like the simple while.

    from: http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming 

    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.
     from: http://www.answers.com/Q/Advantage_and_disadvantage_of_for_loop_over_while_looop 

    Which data structure do you consider to be more robust? Why?

    - NOt sure 

    Nov 10th, 2014

    Did you know? You can earn $20 for every friend you invite to Studypool!
    Click here to
    Refer a Friend
    ...
    Nov 10th, 2014
    ...
    Nov 10th, 2014
    Dec 11th, 2016
    check_circle
    Mark as Final Answer
    check_circle
    Unmark as Final Answer
    check_circle
    Final Answer

    Secure Information

    Content will be erased after question is completed.

    check_circle
    Final Answer