ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 7 - 생활코딩 자바강의 정리(조건문)
    TIL 2023. 1. 16. 23:33

    [목차]

    1. 조건문 if
    2. 조건문 else
    3. 조건문 응용, 조건문의 중첩

    조건문 if

    조건문

    • 주어진 조건에 따라서 애플리케이션을 다르게 동작하도록 하는 것 입니다.
    • 비교 연산의 결과로 true or flase을 얻을 수 있습니다.
    // 조건문에서 if절에 true일 때 then절을 실행 합니다.
    // if절이 false면 then절을 실행하지 않습니다.
    if(true) { // if절
    // then절 	
    }
    if(true) {
    	System.out.println(1);
    	System.out.println(2);
    	System.out.println(3);
    	System.out.println(4);
    }
    	System.out.println(5);
    // 값
    // 1
    // 2
    // 3
    // 4
    // 5
    if(false) {
    	System.out.println(1);
    	System.out.println(2);
    	System.out.println(3);
    	System.out.println(4);
    }
    	System.out.println(5);
    // 값
    // 5

    조건문 else

    조건문 else

    • if-else절은 if 절의 값이 true일 때 then절이 실행되고, flase일 때 else절이 실행됩니다.
    if(true) {
    	System.out.println(1);
    } else {
    	System.out.println(2);
    }
    // 값: 1출력
    
    if(flase) {
    	System.out.println(1);
    } else if (true) {
    	System.out.println(2);
    } else if (true) {
     	System.out.println(3);
    } else {
    	System.out.println(4);
    }
    // true가 한번이라도 발견되면 처음으로 등장한 구간이 실행되고 if문은 종료가 됩니다.
    // 값: 2출력

     

    조건문 응용, 조건문의 중첩

    조건문 응용, 조건문의 중첩

    // 사용자가 프로그램 실행 전 미리 값을 정해두어 실행 시 프로그램에 전달하는 인자
    // 가볍게 넘어가도 됩니다.
    String id = args[0]; // 입력값
    // equals : equals점 앞에 붙어있는 값과 equals괄호안에 있는 값을 비교함
    // 사용자가 입력한 값과 "egoing"가 같다면 true 출력
    // true일 시 right 출력
    if(id.equals("egoing") {
    	System.out.println("right")
    } else {
    	System.out.println("wrong")
    }
    	// 사용자에게 "egoing"값을 받으면 밑에 있는 if문 실행
        // password가 true이면 right 실행 false이면 wrong실행
        // id가 egoing가 아닐 시 맨 밑에있는 else문에 있는 wrong실행
        String id args = [0]
    	String password = args[1]
    	if (id.equals("egoing")) {
    		if (password.equals("111111")) {
        		System.out.println("right");
        	} else {
        		System.out.println("wrong");
        	}
      
    	} else {
        	System.out.println("wrong");
        }
Designed by Tistory.