티스토리 뷰

코딩/오류 노트

if와 else if 차이

ehzim 2023. 10. 11. 18:03
package chapter4;

public class report_2 {

	public static void main(String[] args) 
	{
		int point = 85;
		
		
		if(point>90) 
		{
			System.out.println("A");
			
		}
		
		if(point<=90 && point>80) 
		{
			System.out.println("B");
		
		}
		
		if(point<=80 && point>70) 
		{
			System.out.println("C");
			
		}
		
		else
		{
			System.out.println("F");
		}

	} //if로 전부 사용하면 첫번에 if 안되면 두번째 if 실행 마지막 if 또는 else 출력
	//그래서 성적 산출같은 프로그램은 여러개 중 하나의 출력값을 내야하니까 else if가 맞음.




}
B
F

 

 

package chapter4;

public class report_2 {

	public static void main(String[] args) 
	{
		int point = 85;
		
		
		if(point>90) 
		{
			System.out.println("A");
			
		}
		
		else if(point<=90 && point>80) 
		{
			System.out.println("B");
		
		}
		
		else if(point<=80 && point>70) 
		{
			System.out.println("C");
			
		}
		
		else
		{
			System.out.println("F");
		}

	} 


}
B

 

 

if는 하나 하나의 if문을 실행하고 출력할까 말까하지만 

if-else if문은 여러 가지의 조건 중 해당하는 하나의 작업을 실행하고 출력한다.

'코딩 > 오류 노트' 카테고리의 다른 글

함수명 일치  (0) 2023.10.17
명령어 위치에 따른 오류  (0) 2023.10.13
break;로 인한 오류  (0) 2023.10.12
세미콜론으로 인한 오류  (0) 2023.10.12
중괄호 위치에 따른 오류  (0) 2023.10.11
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday