본문 바로가기

JAVA/기초다지기

(45)
조건문(응용) package org.opentutorials.javatutorials.condition; public class LoginDemo { public static void main(String[] args) { String id = args[0]; if(id.equals("egoing")){ System.out.println("right"); } else { System.out.println("wrong"); } } } args[](입력값) -> Run configurations(이프로그램을 실행할때 설정을 바꿀 수 있음) -> Arguments(입력값)
조건문(else) if 만으로는 좀 더 복잡한 내용을 처리하는데 부족하다. if-else절은 if 절의 값이 true일 때 then절이 실행되고, false일 때 else절이 실행된다. if (false) { System.out.println(1); } else { System.out.println(2); } } } package org.opentutorials.javatutorials.condition; public class Condition3Demo { public static void main(String[] args) { if (true) { System.out.println(1); } else { System.out.println(2); } } } < if(fa..
조건문 (if) : 조건문이란 주어진 조건에 따라서 애플리케이션을 다르게 동작하록 하는 것 (프로그래밍의 핵심) 조건문의 문법 프로그래밍에서 문(文, Statements)은 문법적인 완결성을 가진 하나의 완제품이라고 할 수 있다. if문, for문, while문등이 여기에 해당한다. 절(節마디절, clause)은 문(statements)를 구성하고 있는 부품이라고 할 수 있다. if 조건문은 if로 시작한다. if 뒤의 괄호를 if절이라고 부르고, 중괄호가 감싸고 있는 구간을 then 절이라고 부르겠다. 조건문에서는 if 절의 값이 true일 때 then 절이 실행된다. if 절이 false이면 then 절은 실행되지 않는다. if(조건식){ 실행문; 실행문; } 조건식이 true값을가질 때 중괄호({ }),then절 ..
Boolean과 Comparison Operator Boolean : 참과 거짓을 의미하는 데이터 타입으로 ' bool ' 이라고도 불림. boolean은 정수나 문자와 같이 하나의 데이터 타입인데, 참을 의미하는 true와 거짓을 의미하는 false 두 가지의 값을 가지고 있다. Comparison Operator: 비교연산자는 관계연산자로도 불림. 프로그래밍에서 비교란 주어진 값들이 같은지 다른지,큰지, 작은지를 구분하는 것을 의미. 이를 위해 비교 연산자를 사용하는데 비교연산자의 결과는 True/ False 중 하나다. ' == ' : 좌항과 우항을 비교해서 서로 값이 같다면 True 또는 False가 된다. package org.opentutorials.javatutorials.eclips; public class EqualDemo { public..
연산자(단항연산자) package org.opentutorials.javatutorials.eclips; public class PrePostDemo { public static void main(String[] args) { int a = 4-3*6; System.out.println(a); //console -> -14 } } 위의 구문에는 3가지의 연산자가 등장한다. =, -, * 이다. 표에 따라서 우선순위 별로 배열해보면 *, -, =가 된다. 그러므로 연산자 *가 제일 먼저 실행된다. 따라서 첫 번째 연산은 3*6이 된다. 그 값은 18이다. 그 다음 우선순위는 -다. 4-18을 해야 하는데 빼기의 결합 방향은 →이다. 따라서 4에서 18을 빼야 한다. 그 결과는 -14가 된다. 그다음 우선순위는 대입 연산자인 ..
연산자(형변환) package org.opentutorials.javatutorials.operator; public class DivisionDemo { public static void main(String[] args) { int a = 10; int b = 3; float c = 10.0F; float d = 3.0F; System.out.println(a/b); System.out.println(c/d); System.out.println(a/d); } } 3 3.3333333 3.3333333 첫 번째 결과는 정수와 정수를 나눈 것 3은 나머지의 몫이고, 나머지는 버려졌다. 정수는 소수점을 표현할 수 없으므로 정수만 표시된 것 세 번째 결과는 정수에서 실수를 나눈 것이다. 이 경우 암시적으로 형 변환이 일어나..
산술연산자(Arithmetic Operator) 사칙연산 = 산술 연산자. +(더하기), -(빼기) /(나누기), *(곱하기), %(나머지 구하기) package org.opentutorials.javatutorials.eclips; public class RemainerDemo { public static void main(String[] args) { int a = 3; System.out.println(0%a); System.out.println(1%a); System.out.println(2%a); System.out.println(3%a); System.out.println(4%a); System.out.println(5%a); System.out.println(6%a); } } 0 1 2 0 1 2 0 나머지는 오른쪽의 피연산자의 값을 왼쪽..
연산자 보호되어 있는 글입니다.