제어문 (flow control)
프로그래밍-시간의 순서에 따라 일어나는 일을 컴퓨터에게 명령하는 것
그런데 시간의 순서만으로 해결할수 없는 문제가 생김
e.g.)로그인->성공시 정보 보여줌/실패시 접근 금지
이런 작업의 경우 조건에 따라 실행되는 순서 제어->조건문 conditional statement 사용
e.g.)1억건의 데이터 처리->반복문 looping statement 사용
boolean data taype
comparison operator
<Boolean data type>
-true/false 값만 가능
package javaflowcontrol;
public class BooleanApp {
public static void main(String[] args) {
System.out.println("One");
System.out.println(1);
System.out.println(true);
System.out.println(false);
String jesus = "Hello world";
// String true = "Hello World"; reserved word
System.out.println(jesus.contains("world"));
System.out.println(jesus.contains("egoing"));
}
}
<비교연산자 comparison operator>
package javaflowcontrol;
public class ComparionOperatorApp {
public static void main(String[] args) {
System.out.println(1>1); //false
System.out.println(1 == 1); //true
System.out.println(1<1); // false
System.out.println(1>=1); // true
}
}
<조건문 conditional statements>
package javaflowcontrol;
public class IfApp {
public static void main(String[] args) {
System.out.println("a");
// if(false) {
// System.out.println(1);
// } else {
// if(true) {
// System.out.println(2);
// } else {
// System.out.println(3);
// }
if(false) {
System.out.println(1);
} else if(true) {
System.out.println(2);
} else {
System.out.println(3);
}
System.out.println("b");
}
}
<조건문 응용 1>
package javaflowcontrol;
public class AuthApp {
public static void main(String[] args) {
System.out.println(args[0]);
String id = "egoing";
String inputId = args[0];
System.out.println("Hi.");
//if(inputId == id) {
if(inputId.equals(id)) {
System.out.println("Master!");
} else {
System.out.println("Who are you?");
}
}
}
<조건문 응용 2>
package javaflowcontrol;
public class AuthApp {
public static void main(String[] args) {
System.out.println(args[0]);
String id = "egoing";
String inputId = args[0];
String pass = "1111";
String inputPass= args[1];
System.out.println("Hi.");
//if(inputId == id) {
// if(inputId.equals(id)) {
// if(inputPass.equals(pass)) {
// System.out.println("Master!");
// } else {
// System.out.println("Wrong password!");
// }
// } else {
// System.out.println("Who are you?");
// }
if(inputId.equals(id) && inputPass.equals(pass)) {
System.out.println("Master!");
} else {
System.out.println("Who are you?");
}
}
}
drag+ctrl+/
if(inputId.equals(id))
&& 논리연산자
<== vs equals>
primitive: boolean int double short long float char
non primitive: String Array Date File ...
==: 같은 곳에 있는가?
o1.equals(o2): 내용이 같은가?
primitive data type 은 == 쓰고, non primitive는 equals 쓰면 됨
*string 은 non primitive 임에도 불구하고 워낙 많이 쓰여서 특혜를 받는데 그냥 equals 써라
<반복문 looping statement>
package javaflowcontrol;
public class LoopApp {
public static void main(String[] args) {
System.out.println(1);
System.out.println("=== while ===");
int i = 0;
//.. 많은 내용이 들어가 i의 값이 변형될 수도 있음
while(i < 3) {
System.out.println(2);
System.out.println(3);
// i = i + 1;
//.. 많은 내용이 들어가 i의 값이 변형될 수도 있음
i++;
}
System.out.println("=== for ===");
for(int j = 0; j < 3; j++) { // 하나로 응집되어 문제가 생길 가능성이 적고,
// 코드 의도를 파악하기 좋음
System.out.println(2);
System.out.println(3);
}
System.out.println(4);
}
}
<배열 Array>
반복문과 뗄수 없는 관계
package javaflowcontrol;
public class ArrayApp {
public static void main(String[] args) {
//egoing, jinhuck, youbin
// String users = "egoing, jinhuck, youbin";
String [] users = new String[3];
users[0] = "egoing";
users[1] = "jinhuck";
users[2] = "youbin"; // 0,1,2 원하는 값을 찾을때 사용하는 색인=index
//egoing, jinhuck, youbin 각 배열의 값을 원소=element
System.out.println(users[0]);
System.out.println(users.length); //3칸 짜리 배열이라는 결과가 나옴
//위에 처럼 할수도 있지만 아래처럼 하면 더 빠르고 쉽게 가능
int[] scores = {10, 100, 100};
System.out.println(scores[0]);
System.out.println(scores[1]);
System.out.println(scores[2]);
System.out.println(scores.length);
}
}
0,1,2 원하는 값을 찾을때 사용하는 색인=index
egoing, jinhuck, youbin 각 배열의 값을 원소=element
<반복문(Loop) + 배열(Array)>
package javaflowcontrol;
public class LoopArray {
public static void main(String[] args) {
/*
* <li>egoing</li>
* <li>jinhuck</li>
* <li>youbin</li>
*/
String [] users = new String[3];
users[0] = "egoing";
users[1] = "jinhuck";
users[2] = "youbin";
for(int i=0; i<users.length; i++) {
// System.out.println("<li>"+users[i]+"</li>");
System.out.println(users[i]+",");
}
}
}
<제어문 종합 응용 1>
package javaflowcontrol;
public class AuthApp3 {
public static void main(String[] args) {
String[] users = {"egoing", "jinhuck", "youbin"};
String inputId =args[0];
boolean isLogined = false;
for(int i=0; i<users.length; i++) {
String currentId = users[i];
if(currentId.equals(inputId)) {
isLogined = true;
break;
}
}
System.out.println("Hi,");
if(isLogined) {
System.out.println("Master!!");
} else {
System.out.println("Who are you?");
}
}
}
<제어문 종합 응용 2>
package javaflowcontrol;
public class AuthApp3 {
public static void main(String[] args) {
// String[] users = {"egoing", "jinhuck", "youbin"};
String[][] users = {
{"egoing", "1111"},
{"jinhuck", "2222"},
{"youbin", "3333"}
};
String inputId = args[0];
String inputPass = args[1];
boolean isLogined = false;
for(int i=0; i<users.length; i++) {
String[] current = users[i];
if(
current[0].equals(inputId) &&
current[1].equals(inputPass)
) {
isLogined = true;
break;
}
}
System.out.println("Hi,");
if(isLogined) {
System.out.println("Master!!");
} else {
System.out.println("Who are you?");
}
}
}
'입문 > 생활코딩' 카테고리의 다른 글
JAVA - method (0) | 2022.12.08 |
---|---|
JAVA (0) | 2022.11.27 |
WEB1-HTML & Internet (0) | 2022.11.23 |