Java Beginner: Lesson 6
do while loop
Same as the while loop, shown in the lesson 5, there are other loops which make possible to repeat an action or a set of actions. In this lesson we will cover the do while loop.
In the main method which in the previous lesson was as follow:
Example code:
We will make the changes as shown bellow.
In this case, with do while loop, we will have the same result with while loop. The difference would be if insted of true we would put false in the while condition. In the while case if we would put false within the rounded brackets the code inside the currly brackets we would get a compilatin errore because wouldn't have been executed at all because the condition is tested before the start of the execution, meanwhile in the case of do while loop even though we put false in the while condition the code gets executed one time and than exits the loop.
In the figure 1 bellow is shown the case when within the while condition is puted true so we can repeat the action infinite times.
In the figure 2 is shown the execution when in the while condition is false, which means that the code between the do and while keyword will be executed only once.
(please report broken link in the comment)
Comments
Post a Comment