Development/OOP(Java)

Ch3. Flow of Control

일단하고봐 2023. 4. 4. 01:14

Flow of Control (제어 흐름)

branching 과 looping 두가지를 참고할 수 있다.

branching 과 looping은 boolean expressions 로 control 된다.

Branching mechanism :

-if , else if, else

-switch

Compound Statements

→ 명령어를 여러 줄 쓰는 경우 braces({ }) 기호 사용

else는 생략 가능

The Conditional Operator

if (n1 > n2) max = n1;
else         max = n2;

max = (n1 > n2) ? n1 : n2;   (conditional operator)

Boolean Expressions

  • and - && , or - || , not - !

-&, | 로도 사용 가능

*Short-Circuit

:&&의 경우 처음이 false이면 반드시 false이므로 이후 계산 수행x, ||의 경우 처음이 true이면 반드시 true이므로 이후 계산 수행x

→ 프로그램 실행 시간을 줄여줌(효율적) , 런타임 에러를 줄여준다.

ex1)

→ if, else if, else 를 사용하여 입력받은 값에 따른 tax 값을 계산해서 출력하는 프로그램

ex2)

→ switch 문을 사용하여 각각의 경우에 따른 출력값을 출력

-case2,3 처럼 비어 있는 경우 다음으로 넘어가서 case 4의 내용을 출력

-default는 해당 값이 없는 경우에 대해 출력

Using == with Strings

equality comparison을 위해 (==) 연산자를 사용한다.

이때, primitive type에서는 두 값을 비교하게 된다.

하지만 String class의 경우(==)연산자는 값을 비교하는 것이 아니라 같은 메모리 location을 가리키는 것인지 확인하는 용도로 사용된다.

따라서, String class의 값을 비교하기 위해서는 equals, eqaulsIgnoreCase와 같은 method를 사용하여 확인해야 한다.

Loops

-while, do-while, for statements 가 있다.

-loop에서 반복되는 부분의 코드를 body라고 한다.

-body 중에서 반복하는 부분을 iteration이라고 한다.

*while과 do-while의 차이

→while은 조건을 먼저 확인 후 body를 실행한다. 하지만, do-while의 경우 body를 먼저 실행한 뒤 조건을 evaluation한다. 따라서 do-while의 경우 반드시 1번은 body문이 실행된다.

ex3)

package chapter3;

public class chapter3_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int countDown;
		
		System.out.println("first while loop:");
		countDown = 3;
		while(countDown > 0) {
			System.out.println("hello");
			countDown = countDown -1;
			
		}
		
		System.out.println("second while loop:");
		countDown = 0;
		while(countDown > 0) {
			System.out.println("hello");
			countDown = countDown -1;
			
		}
		
		System.out.println("first do-while loop:");
		countDown = 3;
		do
		{
			System.out.println("hello");
			countDown = countDown -1;
			
		}while(countDown>0);
		
		System.out.println("second do-while loo:");
		countDown = 0;
		do
		{
			System.out.println("hello");
			countDown = countDown - 1;
			
		}while(countDown > 0);
		

	}

}

출력결과

Flow of Control (제어 흐름)

branching 과 looping 두가지를 참고할 수 있다.

branching 과 looping은 boolean expressions 로 control 된다.

Branching mechanism :

-if , else if, else

-switch

Compound Statements

→ 명령어를 여러 줄 쓰는 경우 braces({ }) 기호 사용

else는 생략 가능

The Conditional Operator

if (n1 > n2) max = n1;
else         max = n2;

max = (n1 > n2) ? n1 : n2;   (conditional operator)

Boolean Expressions

  • and - && , or - || , not - !

-&, | 로도 사용 가능

*Short-Circuit

:&&의 경우 처음이 false이면 반드시 false이므로 이후 계산 수행x, ||의 경우 처음이 true이면 반드시 true이므로 이후 계산 수행x

→ 프로그램 실행 시간을 줄여줌(효율적) , 런타임 에러를 줄여준다.

ex1)

→ if, else if, else 를 사용하여 입력받은 값에 따른 tax 값을 계산해서 출력하는 프로그램

ex2)

→ switch 문을 사용하여 각각의 경우에 따른 출력값을 출력

-case2,3 처럼 비어 있는 경우 다음으로 넘어가서 case 4의 내용을 출력

-default는 해당 값이 없는 경우에 대해 출력

Using == with Strings

equality comparison을 위해 (==) 연산자를 사용한다.

이때, primitive type에서는 두 값을 비교하게 된다.

하지만 String class의 경우(==)연산자는 값을 비교하는 것이 아니라 같은 메모리 location을 가리키는 것인지 확인하는 용도로 사용된다.

따라서, String class의 값을 비교하기 위해서는 equals, eqaulsIgnoreCase와 같은 method를 사용하여 확인해야 한다.

Loops

-while, do-while, for statements 가 있다.

-loop에서 반복되는 부분의 코드를 body라고 한다.

-body 중에서 반복하는 부분을 iteration이라고 한다.

*while과 do-while의 차이

→while은 조건을 먼저 확인 후 body를 실행한다. 하지만, do-while의 경우 body를 먼저 실행한 뒤 조건을 evaluation한다. 따라서 do-while의 경우 반드시 1번은 body문이 실행된다.

ex3)

package chapter3;

public class chapter3_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int countDown;
		
		System.out.println("first while loop:");
		countDown = 3;
		while(countDown > 0) {
			System.out.println("hello");
			countDown = countDown -1;
			
		}
		
		System.out.println("second while loop:");
		countDown = 0;
		while(countDown > 0) {
			System.out.println("hello");
			countDown = countDown -1;
			
		}
		
		System.out.println("first do-while loop:");
		countDown = 3;
		do
		{
			System.out.println("hello");
			countDown = countDown -1;
			
		}while(countDown>0);
		
		System.out.println("second do-while loo:");
		countDown = 0;
		do
		{
			System.out.println("hello");
			countDown = countDown - 1;
			
		}while(countDown > 0);
		

	}

}

출력결과

Flow of Control (제어 흐름)

branching 과 looping 두가지를 참고할 수 있다.

branching 과 looping은 boolean expressions 로 control 된다.

Branching mechanism :

-if , else if, else

-switch

Compound Statements

→ 명령어를 여러 줄 쓰는 경우 braces({ }) 기호 사용

else는 생략 가능

The Conditional Operator

if (n1 > n2) max = n1;
else         max = n2;

max = (n1 > n2) ? n1 : n2;   (conditional operator)

Boolean Expressions

  • and - && , or - || , not - !

-&, | 로도 사용 가능

*Short-Circuit

:&&의 경우 처음이 false이면 반드시 false이므로 이후 계산 수행x, ||의 경우 처음이 true이면 반드시 true이므로 이후 계산 수행x

→ 프로그램 실행 시간을 줄여줌(효율적) , 런타임 에러를 줄여준다.

ex1)

→ if, else if, else 를 사용하여 입력받은 값에 따른 tax 값을 계산해서 출력하는 프로그램

ex2)

→ switch 문을 사용하여 각각의 경우에 따른 출력값을 출력

-case2,3 처럼 비어 있는 경우 다음으로 넘어가서 case 4의 내용을 출력

-default는 해당 값이 없는 경우에 대해 출력

Using == with Strings

equality comparison을 위해 (==) 연산자를 사용한다.

이때, primitive type에서는 두 값을 비교하게 된다.

하지만 String class의 경우(==)연산자는 값을 비교하는 것이 아니라 같은 메모리 location을 가리키는 것인지 확인하는 용도로 사용된다.

따라서, String class의 값을 비교하기 위해서는 equals, eqaulsIgnoreCase와 같은 method를 사용하여 확인해야 한다.

Loops

-while, do-while, for statements 가 있다.

-loop에서 반복되는 부분의 코드를 body라고 한다.

-body 중에서 반복하는 부분을 iteration이라고 한다.

*while과 do-while의 차이

→while은 조건을 먼저 확인 후 body를 실행한다. 하지만, do-while의 경우 body를 먼저 실행한 뒤 조건을 evaluation한다. 따라서 do-while의 경우 반드시 1번은 body문이 실행된다.

ex3)

package chapter3;

public class chapter3_3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int countDown;
		
		System.out.println("first while loop:");
		countDown = 3;
		while(countDown > 0) {
			System.out.println("hello");
			countDown = countDown -1;
			
		}
		
		System.out.println("second while loop:");
		countDown = 0;
		while(countDown > 0) {
			System.out.println("hello");
			countDown = countDown -1;
			
		}
		
		System.out.println("first do-while loop:");
		countDown = 3;
		do
		{
			System.out.println("hello");
			countDown = countDown -1;
			
		}while(countDown>0);
		
		System.out.println("second do-while loo:");
		countDown = 0;
		do
		{
			System.out.println("hello");
			countDown = countDown - 1;
			
		}while(countDown > 0);
		

	}

}

출력결과