Questions
Give an example of a while loop, then provide the equivalent do-while loop and for loop. Then give a different example of a do-while loop, along with the equivalent while loop and for loop. Finally, give an example of a for loop, along with the equivalent while loop and do-while loop. Use your examples to illustrate the advantages and disadvantages of each looping structure, and describe those advantages and disadvantages.
1. Examples
1.1. Examples for a while loop, the equivalent do-while loop and for loop
1.1.1 A while loop
Codes:
1 | package discussion_assignment_1; |
Outputs:
1 | The product of positive integers 1 to 10 is: 3628800 |
1.1.2 The equivalent do-while loop
Codes:
1 | package discussion_assignment_1; |
Outputs:
1 | The product of positive integers 1 to 10 is: 3628800 |
1.1.3 The equivalent for loop
Codes:
1 | package discussion_assignment_1; |
Outputs:
1 | The product of positive integers 1 to 10 is: 3628800 |
1.2. Examples for a do-while loop, the equivalent while loop and for loop
1.2.1. A do-while loop
Codes:
1 | package discussion_assignment_2; |
Outputs:
1 | The number of times you tried on clothes:1 |
1.2.2. The equivalent while loop:
Codes:
1 | package discussion_assignment_2; |
Outputs:
1 | The number of times you tried on clothes:1 |
1.2.3. The equivalent for loop
Codes:
1 | package discussion_assignment_1; |
Outputs:
1 | The product of positive integers 1 to 10 is: 3628800 |
1.3. Examples for a for loop, the equivalent while loop and do-while loop
1.3.1. A for loop
Codes:
1 | package discussion_assignment_3; |
Outputs:
1 | JAVA |
1.3.2. The equivalent while loop:
Codes:
1 | package discussion_assignment_3; |
Outputs:
1 | JAVA |
1.3.3. The equivalent do-while loop
Codes:
1 | package discussion_assignment_3; |
Outputs:
1 | JAVA |
2. Advantages and Disadvantages
According to Downey, “It’s quite possible that in real programs, for loops actually outnumber while loops” (2019, p. 92). In other words, for loop is more commonly used than while loop. This is because for loop has some advantages that other loop does not own: “The initialization, continuation condition, and updating have all been combined in the first line of the for loop. This keeps everything involved in the “control” of the loop in one place, which helps make the loop easier to read and understand” (Downey, 2019, p. 92).
Conversely, the while loop and do-while loop do not put initialization, continuation condition, and updating in the same row, so they are not as easy to read and understand as for loop.
For instance, in the example of calculating the product of positive integers 1 to 10, the for loop put “int i = 1; i < 11; i ++” in one line, but the while loop and do while loop put them at different places, respectively. Hence, to some extent, the while loop and do-while loop are not as concise as the for loop.
Regrading the do-while loop, “since the condition is not tested until the end of the loop, its body is always executed at least once” (Downey, 2019, p. 89). Therefore, sometimes it is convenient to use a do-while loop for tasks that must be done at least once.
For instance, when buying clothes, we usually try at least one time before we decide to buy them. Hence, it is more suitable to use a do-while loop instead of for loop or while loop in this case. As we can see, in the example of trying on clothes some times before buying them, the codes using the do-while loop are much less than the codes using the while loop. Therefore, in some situations, the do-while loop is more advantageous.
Word count: 1243
References
1.Downey, A. (2015). Think Python. Green Tea Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf
2.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes