Java Beginner: While loop

Source: Internet
Author: User

Last time we talked about a for loop, in a nutshell, A for loop is generally useful for controlling the number of executions of certain operations. But in fact, we are dealing with the actual problem, and we often encounter another situation where we need to use loops: that is, when certain conditions are set up, we perform certain actions. For example: When the rain on the day, I have been at home to sleep, when I feel bored, I have been looking at crayon small new, and so on, and so on. So how does this loop behave in Java, and that's the while loop we're going to talk about today. The format of the while loop in Java is this:

Initialize a condition (most of the time)

while (a condition is established)

{

Perform certain operations;

Change the condition in the while (most of the time you need to change, but there are some times you don't have to change)

}

The above is the while loop, in order to better illustrate the problem, we make a user input small program, this applet, as long as the user input Yes, then Java will always print out "Hello Baby", then Java will prompt the user to continue to enter the content, As long as the user enters no longer yes, it jumps out of the while loop. As shown below:

We can see that when we enter Yes, it prints out Hello baby, regardless of the case, because the reason for the equalsignorecase function is to ignore the size-heart comparison. When we enter a not yes, we jump out of the while loop and continue to run the program after the while. The run order of the While loop is this:

1. First check the condition inside the while, if true, enter the curly brace below the while and perform the operation inside. If False, skip the while loop and continue running the subsequent program.

2. If you enter a while loop, then after the operation, return to the parentheses after the while to check if that condition is true, then execute the operation inside the curly braces, not the words to jump out of the loop to continue the subsequent program.

3. Repeat the second step until you jump out of the while loop.

It is important to change the decision condition behind the while loop, if it is not changed in the curly brackets of the while cycle, it is possible to cause the while loop to execute forever, that is, the legendary dead loop, which we can explain later, but tell you now, The cycle of death is the constant cycle of no termination. Let's take a look at the following program, we just have to change the above program slightly, is the following code:

You see, have been printing Hello baby, endless. If this is the case, do not worry, direct CTRL + C can be forced to quit the program.

The dead loop is bad, it is due to logic error, that is logical error, this error is artificial, the Java compiler can not detect it. The Java compiler can only detect syntax errors, which is the grammatical error of writing.

So, is it not in the while loop that you must change the criteria for while? In fact, this is not absolute. Because actually, we can use break to force out of the loop, or if while is in a function, then we can also use return to jump out of the function, of course, we also jump out of the while loop. However, as a beginner, at present, do not care too much about this, if interested can go to see. Continue, break we'll talk later, and return will wait until the function is explained.

Finally, let's use the while loop to do an input checking to end this course. Input checking is the import detection, that is, if the user input data does not meet the program requirements, the program will require users to re-enter until the user input appropriate data. This time we let the user enter a string representing floating-point data, and then use the matches method of string and the regular expression to determine if the user's input is a number, if not it has been in the while loop, if so, then jump out of the while loop, calculate and display the sin (x ), the program and the results of the operation are as follows:

As you can see, the while loop is a good way to detect the user's input, which can be considered an application of the while loop. Although the while loop has a lot to say, but afraid to say the messy, or pull too far, so today first to come here, next time we have to continue to talk about the loop, because the loop in any computer language is very important, of course, including Java.

Java Beginner: While loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.