image
Do-while Loops
Start course
Difficulty
Beginner
Duration
3h 15m
Students
118
Ratings
4.6/5
Description

In this course, we look at control flow statements in the Java programming language. 

Learning Objectives

  • Decision-making statements
  • Looping statements
  • Branching statements 

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge
Transcript

Hi there, in this video, we'll talk about the do-while loop. The do-while loop is similar to the while loop but there is only one difference between them. The do-while loop checks the condition after executing the statements in the loop body, that means the body always executes at least once. There is no checking of any condition for the first time. You got it. This is the main difference between the while loop and the do-while loops. After the execution of the statements in the loop body, the value of the variable is updated and finally the condition is checked for the true or false value. If the condition is true, the next iteration of the loop starts. If the condition is false, the loop terminates and jumps to the next statement after the do-while loop. The statements in the loop body are executed at least once before condition controlling, this is an important point. Let's make some examples with the do-while loop. 

In exercise project, right click on the loop package and select new and class, specify the class name as do-while loop and select the checkbox for the main method. Let's write a program that prints int numbers from 1 to 15. Let's declare a variable named number and the initial value will be one. Let's start creating the do-while loop with the do keyword. In the loop, we use the print method to display numbers. I'll print them side by side for this, I'll use the print method not println. And we need to update the variable number. So, we increase the value of the number by one. Now, we add a loop condition with the while keyword. Our condition is the number is less than or equal to 15. Let's run the code. You see the numbers from 1 to 15 on the screen. In the do-while loop the print process and increment part of the body is executed at least once before the condition. Let's understand this; if I change the condition in the while as false the code inside the do block will execute once and since the condition is false, the loop will terminate. So, the code will be executed at once and we'll see the one on the console.

Let's run the code and see the difference. As you can see, the code in the do block is executed once and the output is one, then it checked the condition so the condition is false and the loop terminates. Let's write a program that chooses a fruit from the menu. So, we need to take inputs from the user by using the scanner class, scanner input, new scanner system.in. We choose the import scanner class in java.util package to import the scanner class. Let's declare a variable choice with no initial value. Let's start to write the do-while loop. Inside the loop, first we write a title for the title of the program. The title can be menu with uppercase. Let's copy the print method and paste it below five times. Now, let's make some changes. The 1st option on the menu can be apple, the 2nd is banana, the 3rd is orange and the last option can be press other keys to exit. In the last print method we give the select an option message to the user and this method will be the print method, not println. Now, we'll assign the next method of the scanner class to the variable choice. Also, I'll use the switch case statement in this part. It's useful for menu operations. 

You can use all control flow statements or loops in each other. What you should watch out for are tricks like conditions, what cycle starts and when. Let's create the switch case. The conditional switch expression will be the variable choice. In the first case we display apple in the print method and we add the break keyword. Let's copy the code and make changes for case values and other fruits. The second will be banana and the 3rd will be orange and I will not use the default case here. It is not mandatory for the switch case. Now, let's write the condition with the while keyword. If the user selects an option from the menu, the loop should continue but if the user presses the different keys on the keyboard, the loop should terminate. 

So, this loop is executed until the choice is greater than or equal to one, and operator, the choice is less than or equal to three. Finally, to indicate that the program has terminated, I'll display a message to the user using the println method outside of the do-while loop. S out, program terminated. Let's run the code. You see the menu in the console. Let's enter one, apple is displayed and the program continues. Enter two, banana is displayed and the program continues again. Enter three, orange is displayed and the program continues again. Now, I will press another key on the keyboard as you can see, the program terminated appears on the console because the loop is terminated. Finally, let's make an example about the nested do-while loop and end our lesson. I'll convert all codes to the comment line. I'll create a variable named num and the initial value of this variable can be 20. Now I will create the do-while loop. In the do block, I'll increase the num variable by one and then I'll write the while condition. While num greater than 20, and I'll create a while loop in the do block, while num less than 10. 

I'll decrease the num variable by one in the while loop and I'll display the num variable on the console outside the do-while loop. S out, double quotes, num plus num. Before running the code let's examine the code. The initial value of the num variable is 20. It begins with the do block. The value of num will increase on this line. So, the new value of the num variable will be 21 and it'll check the inner while loop condition. So, the condition is false because 21 is greater than 10, so it skips the inner while loop and continues with the while condition of the do-while loop. So, this condition is true because the new value of the num variable is 21 and 21 is greater than 20, therefore it returns to the do block again. So, this loop will continue forever like this. And as a result, the num variable will never print on the console because the compiler will not exit out of the loop. So, let's run and see. As you can see, the output is empty in fact, currently the do-while loop is executing. Let's observe this. I'll copy this print method and paste it into the inner while loop and inside the do block, I will change this part to num in while loop and this will be the num in do block. Now, let's run and see the output. As you can see, the code inside the do block is running forever. I'll terminate this code. Now, let's change the operators of the inner while loop I'll change it to greater than, now let's examine the code again. The initial value of the num variable is 20. It begins with the do block. The value of num will increase on this line. 

So, the new value of the num variable will be 21 and it'll check the inner while loop condition. So, the condition is true because 21 is greater than 10 and it'll continue with this line. The value of num will decrease on this line. So, the new value of the num variable will be 20 and the second output on the console will be num in while loop 20. And since the condition is true, the code returns to the beginning of the while loop again. And this iteration will continue until the value of the num variable is 10 and this print method will execute 11 times. After the condition of the inner while loop is false, it'll continue with the outer while part of the do-while loop, and it'll print this expression on the console, and the new value of the num variable is 10. So, the condition is false because 10 is not greater than 20. So, the loop will be terminated and the last print method will be executed. 

Let's run and see. As you can see, the first value of the num variable is 20 and it printed the num variable in the inner while loop 11 times. After the num variable is 10, the condition of the inner while loop is false and it exited from the inner while loop and it executed the print method in the do while block. And lastly, it checked the while condition of the do-while loop and since the condition is false, it exited from the loop and printed the last expression. Yes, it may seem complicated, but you may encounter similar examples. That's why I wanted to use this example. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
4005
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics