728x90
반응형
SMALL
- 프로그램 언어에서 실행 순서를 변경하거나 조건에 따라 실행하거나 특정 부분은 반복하거나 할 경우 쓰는 구문
- 프로그램은 일반적으로 위에서 아래로 순차적으로 실행
- 제어문의 종류 조건문, 반복문, 기타 제어문 조건문 : if, switch 반복문 : while, for, do ~ while 기타 제어문 : break, continue
(1) 조건문
- "조건에 따라 달라지는 것"을 구현하는 문장
1) if 문(Statement)
1-1) if (조건) {
실행할 문장;
}
1-2) if (조건) { 실행할 문장; }
else if (조건) { 실행할 문장; }
....
else { 실행할 문장; }
2) switch 문
- switch ~ case
switch(변수){
case 값1 :
실행할 문장;
break;
case 값2 :
실행할 문장;
break;
default :
실행할 문장;
}
package Day02;
public class IfTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
p("If Test");
p("*************");
int a = 1;
if( a > 0 ) {
p("a is positive");
}
}
public static void p(String str) {
System.out.println(str);
}
}
결과값
If Test
************
a is positive
package Day02;
public class IfTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
p("If Test");
p("*************");
int a = -1;
if( a > 0 ) {
p("a is positive");
}
}
public static void p(String str) {
System.out.println(str);
}
}
결과값
If Test
*************
- 1일때 값이 안나오고 그냥 넘어간다.
package Day02;
import java.util.Random;
public class IfTest {
public static void main(String[] args) {
//TODO Auto-generated method stub
p("If test");
p("*************");
int a = 1;
if(a > 0) {
p("a is positive");
}
p("if-else");
int b = -1;
if(b > 0) {
p("b is positive");
}
else {
p("b is not positive");
}
p("if-else if ***");
int c = 60;
if(c > 50) {
p("c is big number");
}
p("if-else if else");
int d = -30;
if(d > 0) {
p("d is positive");
}
else if(d == 0) {
p("d is zero");
}
else {
p("d is negative");
}
// nested(중첩) if statement
p("if-if *** ");
int math = 90;
int eng = 35;
if(math >= 60) {
if(eng >= 60) {
p("Pass");
}
else
p("Fail");
}
else
p("Fail");
// 조건을 && 등의 연산자로 묶을 수 있음
if(math >= 60 && eng >= 60)
p("pass");
else
p("Fail");
// if 문은 {} 괄호 생략 가능
p("***********");
p("switch");
p("***********");
int i = 3;
switch(i) {
case 1 :
p("1인가?");
case 2 :
p("2였네");
case 3 :
p("3맞지");
default :
p("다 아닌데?");
}
Random rd = new Random();
int j = rd.nextInt(3);
switch(j) {
case 0:
p("꽝");
break;
case 1:
p("꽝");
break;
case 2:
p("축!당첨!");
break;
}
}
public static void p(String str) {
System.out.println(str);
}
728x90
반응형
LIST
'개발 > JAVA' 카테고리의 다른 글
Colors (추후 수정 필요 !) (0) | 2023.01.09 |
---|---|
배열 (Array) (0) | 2023.01.09 |
Random (임의의 수를 얻는 도구) (0) | 2023.01.09 |
User Input (사용자로부터 입력을 받는 기능) (0) | 2023.01.09 |
연산(Operation), 연산자(Operator) (0) | 2023.01.09 |