sunhoKim 2021. 7. 8. 17:36

// for

int sum = 0;

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

 

sum += i;

}

System.out.println(sum);

 

주의사항 : for문을 작성할 때, 부동소수점 타입을 사용하면 안된다.

int total = 0;

for(float x = 0.1f; x<=1.0f; x+=0.1f) {

System.out.println(x);

}

결과

0.1

0.2

0.3

0.4

0.5

0.6

0.70000005

0.8000001

0.9000001

 

//중첩 for

for(int m = 2; m<=9; m++) {

for(int n = 1; n<=9; n++) {

System.out.println(m + "x" + n + " = " + (m*n));

}

}

 

//while

while(sum<=10) {

System.out.println(sum);

sum++;

}

 

// do-while

System.out.println("메시지를 입력하세요.");

System.out.println("프로그램을 종료하시려면 q키를 입력하세요.");

 

Scanner sc = new Scanner(System.in);

String inputString;

 

do {

System.out.print(">");

inputString = sc.nextLine();

System.out.println(inputString);

}while(!inputString.equals("q"));

 

System.out.println();

System.out.println("프로그램 종료");

 

 

728x90