Java-while Loop Statement __string

Source: Internet
Author: User

While loop statements are simpler to use than for statements, and are simple in format;

while (judging condition) {
Circulation body
}

public class Whiletest {public
    static void Main (string[] args) {
        int i=1;
        while (i<=10) {
            System.out.println ("Hello" +i);
            i++
        }}
    }

Effect:

Hello 1, Hello
2,
3 Hello
4
Hello 5 Hello 6 Hello 7 Hello 8 Hello 9 Hello 10

Can be used in this way;
while (true) {
Circulation body
}
The advantage of this is that it keeps looping and does not stop.

public class Whiletest {public
    static void Main (string[] args) {
        int i=1;
        while (true) {
            System.out.println ("Hello");}}

Effect:

Hello, hello, hello, hello, hello ...

There is also a doing while with a while, and the difference between the while and the while is the format of the Do while;

do{
Circulation body
}whil (Judgment statement);

The Do while executes the loop-body statement and then evaluates the statement, which means that the loop body statement executes at least once, regardless of whether or not the judgment is true.

public class Whiletest {public
    static void Main (string[] args) {
        int i=1;
        do {
            i++;
            System.out.println (i);
        } while (i<1);
    }
}

Effect:

2

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.