JAVA basics/Lesson 17th: control procedures/java while and DO-WHILE and FOR loop statements, whiledo-while

Source: Internet
Author: User

JAVA basics/Lesson 17th: control procedures/java while and DO-WHILE and FOR loop statements, whiledo-while

1. Repeated execution when the condition is true

As long as the expression in the while clause is true, it will be continuously executed cyclically.

Public class HelloWorld {public static void main (String [] args) {// print 0 to 4 int I = 0; while (I <5) {System. out. println (I); I ++ ;}}}
2. If the condition is true, the execution is repeated at least once.

The difference with while is that, whether it is true or not, it is executed once before judgment.

Public class HelloWorld {public static void main (String [] args) {// print 0 to 4 // The difference with while is that, whether it is true or not, run it once first, then judge int I = 0; do {System. out. println (I); I ++;} while (I <5 );}}
3.

For Loop, same as while, but different expressions

Public class HelloWorld {public static void main (String [] args) {// use while to print 0 to 4 int I = 0; while (I <5) {System. out. println ("while loop output" + I); I ++;} // use for to print 0 to 4 for (int j = 0; j <5; j ++) {System. out. println ("for loop output" + j );}}}

 

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.