1 : for 루프 :
package cn.work;
public class Work1 {
public static void main(String[] args) {
int n = 0;
for (int i = 0; i <=100 ; i++) {
if (i%2==0){
n = n + i;
}
}
System.out.println("100以内的偶数和为"+n);
}
}
2 : while 루프
package cn.work;
public class Work2 {
public static void main(String[] args) {
int i = 0;
int n = 0;
while(i<=100){
i++;
if(i%2==0){
n = n+i;
}
}
System.out.println("100以内的偶数和为"+n);
}
}
3 : do ... while 루프
package cn.work;
public class Work2 {
public static void main(String[] args) {
int i = 0;
int n = 0;
while(i<=100){
i++;
if(i%2==0){
n = n+i;
}
}
System.out.println("100以内的偶数和为"+n);
}
}
작업 결과는 다음과 같습니다.