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