Backend/JAVA2 멘토시리즈

JAVA Eclipse 07 제어문 : 조건문

쏠솔랄라 2023. 3. 21. 20:06

 

 

제어문 : 조건문

: 프로그램의 흐름을 제어하는 문법

 

 

프로그램의 흐름

프로그램은 위에서 아래로, 왼쪽에서 오른쪽으로 실행된다

프로그램은 내가 원하는 조건에 대해 원하는 명령을 실행한다

 

제어문의 종류

조건문 : 정해진 조건에 따라 흐름을 제어하는 문법

반복문 : 정해진 조건에 따라 흐름을 반복하는 문법

 

 

조건문

: 조건에 따라 각각 다르게 실행하도록 만들어 놓은 제어문

 

 

if문

if (조건식) {
조건식이 참일 때 실행할 명령;
}

: 만약 (조건식)이 참이라면 명령을 실행한다

조건문 안의 조건식이 항상 참이라는 것을 뜻한다

 

 

if ~else문

if (조건식) {
조건식이 참일 때 실행할 명령;
} else {
조건식이 거짓을 때 실행할 명령
};

 

 

if ~else if문

if (조건1) {
조건1이 참일 때 실행할 명령;
} else if (조건2) {
조건1이 거짓이고 조건2가 참일 때 실행할 명령;

 

 

중첩 if문

: if문 안에 if문이 존재하는 문법 구조

if (조건1) {
if (조건2){
조건1을 충족하고 조건 2를 충족할 때 실행할 명령;
}
}

 

 

ex.

import java.util.Scanner;

public class if_if {

public static void main(String[] args) {

 

String id, pw;

Scanner input = new Scanner(System.in);

System.out.println("아이디를 입력해 주세요");

id = input.nextLine();

 

if (id.equals("java")) {

System.out.println("id 일치");

System.out.print("비밀번호를 입력해 주세요 : ");

pw = input.nextLine();

 

if (pw.equals("abc123")) {

System.out.println("password 일치");

System.out.println("로그인 성공");

} else {

System.out.println("password 불일치");

}

} else {

System.out.println("id 불일치");

}

}

}

 

 

출력화면

 

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84