본문 바로가기

자바

4. 제어문과 반복문

제어문은 사용자가 원하는 조건에 해당하는 경우 특정 명령을 수행하고 싶을 때 사용한다.

제어문의 구성으로는 if문과 switch문이 있다.

우선 if문을 알아보자.

if문의 구성은 다음과 같다.

 

if(원하는 조건) {

    조건에 맞는 명령을 수행

}

else if(원하는 조건) {

    조건에 맞는 명령을 수행

}

else {

    조건에 맞는 명령을 수행

}

 

if와 else if는 조건을 가지지만 else는 조건을 가지지 않는다는 점을 기억하자.

import java.util.Scanner;

public class if_ex {
	public static void main(String[] args) {
		System.out.println("평점을 입력하세요");
		Scanner sc = new Scanner(System.in);
		double score = sc.nextDouble();
		
		if(100 >= score && score>=90) { //90점이상부터 100점까지는
			System.out.println("A입니다."); //A출력
		}
		else if(89 >= score && score>=80) { //89점이상부터 80점까지는
			System.out.println("B입니다."); //B출력
		}
		else if(79 >= score && score>=70) { //79점이상부터 70점까지는
			System.out.println("C입니다."); //C출력
		}
		else if(69 >= score && score>=60) { //69점이상부터 60점까지는
			System.out.println("D입니다."); //D출력
		}
		else { //나머지 점수는
			System.out.println("F입니다."); //F출력
		}
	}
}

 

이번에는 switch문을 알아보자

switch문의 구성은 다음과 같다.

switch(조건){

case 조건1:

    실행하고 싶은 명령문

    break;

case 조건2:

    실행하고 싶은 명령문

     break;

case 조건3:

    실행하고 싶은 명령문

    break;

.

.

.

.

default:

    실행하고 싶은 명령문

}

 

해당 조건이 실행되면 다음조건이 실행이 되지 않게 break로 탈출한다.

조건에 들어갈 수 있는 변수는 정수 타입 변수와 문자열 타입 변수이다. 문자열 타입의 변수는 JDK7부터 적용되었다.

import java.util.Scanner;

public class Switch_ex {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int check = sc.nextInt();
		String str = sc.next();
		
                //숫자가 기준이 되어서 case에 들어간다.
		switch(check) {
		case 1:
			System.out.println(1);
			break;
		case 2:
			System.out.println(2);
			break;
		case 3:
			System.out.println(3);
			break;
		default:
			System.out.println("1, 2, 3이 아닌 다른 숫자");
			break;
		}
		
                //문자열이 기준이 되어 case에 들어간다.
		switch(str) {
		case "A":
			System.out.println("A");
			break;
		case "B":
			System.out.println("B");
			break;
		case "C":
			System.out.println("C");
			break;
		default:
			System.out.println("A, B, C가 아닌 다른 문자열");
			break;
		}
	}
}

 

 

 

이번에는 반복문에 대해 알아보자

반복문의 구성으로 while문과 for문이 있다.

우선 for문을 알아보자

for문의 구성은 다음과 같다.

 

for(초기값; 조건; 조건에 해당하는 작업){

     실행하고 싶은 명령문

}

public class for_ex {
	public static void main(String[] args) {
		for(int i = 1; i <= 9; i++) { //초깃값은 1이고 i는 9보다 크게 될 때까지 i를 1씩 증가
			System.out.println("2 * " + i + " = " + 2 * i);
		}
	}
}

 

이번에는 while문을 알아보자

while문의 구성은 다음과 같다.

while(조건){

    실행하고 싶은 명령문

}

public class While_ex {
	public static void main(String[] args) {
		int n = 1;
        while(n <= 9) { //n이 9이하일 때 실행
			System.out.println("2 * " + n + " = " + 2 * n);
			n++ //n을 증가
		}
	}
}

 

 

 

이번에는 continue를 알아보자

continue는 특정 조건을 만났을 경우 반복문에 맨 처음으로 돌아갈 때 사용하는 문법이다.

public class for_ex {
	public static void main(String[] args) {
		for(int i = 1; i <= 9; i++) {
			if(i % 3 == 0) { //3의 배수이면
				continue; //반복문에 맨 처음으로 올라간다.
			}
			System.out.println("2 * " + i + " = " + 2 * i);
		}
	}
}

 

 

 

이번에는 break를 알아보자

break는 switch문에서 사용했던 역할과 비슷하다.

특정 조건을 만나면 반복문을 탈출하기 위해 사용한다.

public class for_ex {
	public static void main(String[] args) {
		for(int i = 1; i <= 9; i++) {
			if(i == 5) { //i가 5가 되는 순간
				break; //탈출
			}
			System.out.println("2 * " + i + " = " + 2 * i);
		}
	}
}

 

'자바' 카테고리의 다른 글

6. 절차지향 프로그래밍과 함수 그리고 객체지향1  (0) 2019.07.07
5. 배열  (0) 2019.06.29
3. 연산자  (0) 2019.06.24
2. 자료형  (0) 2019.06.22
1. 자바 시작하기  (0) 2019.06.21