Java Learning notes-looping structure

Source: Internet
Author: User

Syntax examples: (distinguish between their differences, while the first to judge the expression and then start execution, and Do_while is to execute the loop body first, then judge)

while (logical expression) {do{

loop body and//loop body

Iterations//iterations

} while (logical expression)

For (initialization of a variable; logical expression; iteration) {

Loop body

}

The For loop needs to be noted: in the (), separated, not,

Practice:

Calculate 1-100 of all odd and even numbers, respectively?

public class jisuan{

public static void Main (string[] args) {

int Jishu = 0;

int Oushu = 0;

for (int i=0;i<=100;i++) {

if (i%2!=0) {

Jishu + = i;

}else{

Oushu + = i;

}

}

System.out.println ("1-100 Odd and" for: "+jishu+" "+" even and for: "+oushu);

}

}

The number that can be divisible by 5 with a For loop output of 1-1000 and 3 numbers per line?

public class lianxi2{

public static void Main (string[] args) {

for (int j = 0; j<=1000;j++) {

if (j%5==0) {

System.out.print (j+ "\ t");

}

if (j%15==0) {

System.out.println ();

}

}

}

}

Java Learning notes-looping structure

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.