import java.lang.*;
public class ForExample01 {
public static void main(String[] args){
//[1] while 구문을 이용하여 1부터 10까지 출력
int n = 1;
while(n <= 10){
System.out.println("n = " + n);
n++;
}
//[2] for 구문을 이용하여 1부터 10까지 출력
for(int i = 1; i <= 10; i++){
System.out.println("i = " + i);
}
}
}
import java.lang.*;
public class ForExample02 {
public static void main(String[] args){
for(int i=1; i <= 10; i++){
System.out.println("첫 번째 : " + i);
}
for(int i=1; i <= 10; i+=2){
System.out.println("두 번째 : " + i);
}
for(int i=1; i <= 10; i+=5){
System.out.println("세 번째 : " + i);
}
}
}
import java.lang.*;
public class ForExample03{
public static void main(String[] args){
System.out.println("카운트다운");
for(int i=10; i>=0; i--){
System.out.println(i);
}
}
}
for(int i=10; i<=0; i--){ }
import java.lang.*;
public class ForExample04{
public static void main(String[] args){
System.out.println("1부터 10까지 홀수 출력");
for(int i=1; i <= 10; i++){
if(i % 2 != 0){
System.out.println("홀수 : " + i);
}
}
}
}
import java.lang.*;
public class ForExample06 {
public static void main(String[] args){
//1부터 100까지 합계 계산하기
int total = 0;
for(int i = 1 ; i <= 100 ; i++){
total += i;
}
System.out.println("합계 : " + total);
}
}
int total = 0;
total += 1;
total += 2;
total += 3;
total += 4;
total += 5;
/* 중간 생략 */
total += 96;
total += 97;
total += 98;
total += 99;
total += 100;
System.out.println("합계 : " + total);
int total = 0;
for(int i = 1 ; i <= 100 ; i++){
total += i;
}
System.out.println("합계 : " + total);