728x90

 

☕ for

- for문은 반복문 중 하나다

- while문은 변수 선언, 탈출 조건식, 증감식이 3줄로 이뤄지지만, for문은 한줄에 모두 표현한다.

 

☕ for 사용법

for(변수 초기화; 탈출 조건식; 증감식){
	탈출 조건식이 참인 경우 실행되는 부분
}

 

☕ 예제 1

- *을 10번 출력한다

public class ForExam1 {
    public static void main(String[] args) {
        for(int i=0; i<10; i++)
            System.out.println("*");
    }
}

 

☕ 중첩 반복문

- 반복문 안에 조건문이 올 수 있는 것 처럼, 반복문 안에 반복문이 올 수 있다.

 

☕ 중첩 반복문을 이용한 구구단 출력

- 문자열과 더해지면 문자열이 된다

- 문자열 + 정수

  "hello" + 1 ---> "hello1"

- 문자열 + 불린

  "hello" + true ---> "hellotrue"

- 문자열 + 실수

  "hello" + 50.4 ---> hello50.4

 

// 1단 구구단 => 1*9=9

public class Gugudan1 {
    public static void main(String[] args) {
        for(int i=1; i<=9; i++)
            System.out.println("1 *"+ i + "=" + (1*i));
    }
}

 

// 구구단
public class Gugudan2 {
    public static void main(String[] args) {
        for(int k=1; k<=9; k++)
            for(int i=1; i<=9; i++)
                System.out.println(k+"*" +i+ "=" + (k*i));
    }
            System.out.println();
}
728x90

'개발&etc > JAVA' 카테고리의 다른 글

[java] 객체지향문법  (2) 2023.03.08
[java] 반복문과 label  (0) 2023.03.08
[java] 반복문 do while  (0) 2023.03.08
[java] 반복문 while  (0) 2023.03.08
[java] 조건문 Switch  (0) 2023.03.08