본문 바로가기
Framwork/Java기초

Chapter 4. 조건문과 반복문

by 김엉배 2022. 10. 9.
728x90
반응형

1.  불리언 논리 연산자

  • && : and 
  • || : or
  • ! : not
public class Code_43 {
	public static void main(String[] args)
	{
		int a = 10, b = 5, c = 20;
		boolean w, x, y, z;
		w = a > b;
		x = a == c;
		y = (a > b) && (b != c);
		z = !(a == b) || (a < b);
		System.out.println("w : " + w);
		System.out.println("x : " + x);
		System.out.println("y : " + y);
		System.out.println("z : " + z);
	}
}
//결과값
w : true
x : false
y : true
z : true
  • 논리 연산자의 결과는 boolean 자료형에 저장해야 한다.
public class Code_44 {
	public static void main(String[] args)
	{
		int a = 10, b = 5, c = 12, d = 7;
		boolean x;
		x = a + b > c - d && a - 10 == b - 5;
		System.out.println(x);
	}

}
//결과값
true

2. 조건문

  • 조건문은 조건이 참인 경우와 거짓인 경우에 따라 코드의 흐름을 결정할 수 있는 문장이다.

 1)  if 조건문

public class Code_45 {
	public static void main(String[] args)
	{
		int score = 80;
		if (score >= 90) {
			System.out.println("PASS!!");
		}
		System.out.println("ending... program");
	}
}
//결과값
ending... program
  • 위의 코드에서 score 값에 따라서 if 블록이 수행될지를 결정하게 되고, 마지막 줄은 무조건 수행되는 라인이다.
  • if 블록에 한 문장만 있는 경우에는 중괄호를 하지 않아도 되지만, 문장이 두 줄 이상인 경우에는 반드시 중괄호를 해야 한다.
public class Code_46 {
	public static void main(String[] args)
	{
		int score = 85;
		if (score >= 90)
			System.out.println("PASS!!");
		else
			System.out.println("FAIL!!");
		System.out.println("ending... program");
	}

}
//결과값
FAIL!!
ending... program
  • if와 else가 있는 형태는 if조건이 참이면 if블록이 실행되고 else 블록은 수행되지 않는다.
import java.util.Scanner;

public class Code_47 {
	public static void main(String[] args)
	{
		Scanner scin = new Scanner(System.in);
		System.out.print("정수를 입력하시오. : ");
		int x = scin.nextInt();
		System.out.print("실수를 입력하시오. : ");
		double y = scin.nextDouble();
		System.out.print("이름을 입력하시오. : ");
		String name = scin.next();
		System.out.println("x : " + x);
		System.out.println("y : " + y);
		System.out.println("name : " + name);
		scin.close();
	}

}
//결과값
정수를 입력하시오. : 999
실수를 입력하시오. : 2.2
이름을 입력하시오. : test
------------------------
x : 999
y : 2.2
name : test
import java.util.Scanner;

public class Code_48 {
	public static void main(String[] args)
	{
		Scanner scin = new Scanner(System.in);
		System.out.print("Enter a : ");
		int a = scin.nextInt();
		System.out.print("Enter b : ");
		int b = scin.nextInt();
		int max = 0;
		if (a > b) max = a;
		else max = b;
		System.out.println("max : " + max);
		scin.close();
	}

}
//결과값
Enter a : 12
Enter b : 23
max : 23
  • 두 정수를 입력 해야 두 수 중에서 큰 수를 출력한다.
import java.util.Scanner;

public class Code_49 {
	public static void main(String[] args)
	{
		Scanner scin = new Scanner(System.in);
		System.out.print("Enter three numbers : ");
		int a = scin.nextInt();
		int b = scin.nextInt();
		int c = scin.nextInt();
		int max = 0;
		if (a > b) max = a;
		else max = b;
		// c가 max보다 더크면 max를 업데이트함.
		if (c > max) max = c;
		System.out.println("max : " + max);
		scin.close();
	}
}
//결과값
Enter three numbers : 3 4 7
max : 7
  • 서로 다른 세 정수를 입력받아 가장 큰 수를 출력
public class Code_50 {
	public static void main(String[] args)
	{
		int score = 92;
		if (score >= 90)
			System.out.println("Excellent!");
		else if (score >= 80) 
			System.out.println("Good!!");
		else if (score >= 70)
			System.out.println("Ok");
		else
			System.out.println("Not ok");
	}
}
//결과값
Excellent!
  • if , else if, else가 모두 있는 경우

   2) switch 조건문

public class Code_51 {
	public static void main(String[] args)
	{
		int score = 80;
		char grade;
		switch(score) {
		case 90: 
			grade = 'A';
			break;
		case 80: 
			grade = 'B';
			break;
		case 70: 
			grade = 'C';
			break;
		default: 
			grade = 'D';
			break;
		}
		System.out.println("grade : " + grade);
	}
}
//결과값
grade : B
  • switch 옆에 괄호에는 반드시 정수 계열의 변수를 넣어야 한다.
  • break는 switch구문을 끝내라는 키워드다.
public class Code_52 {
	public static void main(String[] args)
	{
		int score = 80;
		switch(score) {
		case 90:
			System.out.println("Your score is 90");
		case 80: 
			System.out.println("Your score is 80");
		case 70:
			System.out.println("Your score is 70");
			break; 
		default:
			System.out.println("Your score is under 70");
		}
	}
}
//결과값
Your score is 80
Your score is 70

 

3. 반복문

  • 반복문은 같은 코드를 여러 번 반복하고자 할 때 사용한다.

   1) while  반복문

while(반복조건)
{
 반복적으로 수행해야 하는 문장
}
public class Code_53 {
	public static void main(String[] args)
	{
		int a = 1;
		int sum = 0;
		while (a <= 10)
		{
			sum += a;
			a += 1;
		}
		System.out.println("sum : " + sum);
	}

}
//결과값
sum : 55
import java.util.Scanner;

public class Code_54 {
	public static void main(String[] args)
	{
		Scanner scin = new Scanner(System.in);
		System.out.print("Enter n : ");
		int n = scin.nextInt();
		int a = 1;
		while (a <= n)
		{
			if (n % a == 0)
				System.out.print(a + " ");
			a += 1;
		}
		scin.close();
	}
}
//결과값
Enter n : 76
1 2 4 19 38 76
import java.util.Scanner;

public class Code_55 {
	public static void main(String[] args)
	{
		Scanner scin = new Scanner(System.in);
		System.out.print("Enter n : ");
		int n = scin.nextInt();
		while (n > 0)
		{
			int m = n % 10;
			System.out.println(m);
			n /= 10;
		}
		scin.close();
	}
}
//결과값
Enter n : 542346
6
4
3
2
4
5

 

 2) for 반복문

-for 반복문의 형태

for(반복문 초기화 부분; 반복조건; 변화)
public class Code_56 {
	public static void main(String[] args)
	{
		int i;
		int sum = 0;
		for ( i = 1; i <= 10; i ++ )
		{
			sum += i;
		}
		System.out.println("sum : " + sum);
	}
}
//결과값
sum : 55
import java.util.Scanner;

public class Code_57 {
	public static void main(String[] args)
	{
		Scanner scin = new Scanner(System.in);
		System.out.print("Enter n : ");
		int n = scin.nextInt();
		int count = 0;
		for (int i = 1 ; i <= n ; i ++)
		{
			if (n % i == 0)
				count ++;
		}
		if (count == 2)
			System.out.println(n + " 은 소수입니다.");
		else
			System.out.println(n + " 은 합성수입니다.");
		scin.close();
	}
}
//결과값
Enter n : 3
3 은 소수입니다.
-----------------
Enter n : 100
100 은 합성수입니다.

 

4.  break와 continue

  • break를 루프 안에서 사용할 때는 루프를 강제로 끝내도록 할 때다. break를 만나면 루프가 끝나게 된다.
public class Code_58 {
	public static void main(String[] args)
	{
		int a = 1;
		while (a <= 10) {
			if (a == 5)
				break;
			System.out.println("a : " + a);
			a++;
		}
	}
}
//결과값
a : 1
a : 2
a : 3
a : 4
------------------------------------------------------
public class Code_59 {
	public static void main(String[] args)
	{
		int a = 1;
		while (a <= 5) {
			if (a == 10)
				break;
			System.out.println("a : " + a);
			a++;
		}
	}
}
//결과값
a : 1
a : 2
a : 3
a : 4
a : 5

  1) continue구문

  • 반드시 루프 안에서만 사용해야 하고, 루프 안에서 다음 반복 단계로 진행하라는 의미를 가지고 있다.
public class Code_60 {
	public static void main(String[] args)
	{
		int a = 0;
		while (a < 10)
		{
			a ++;
			if (a % 3 == 0) continue;
			System.out.println(a);
		}
	}
}
//결과값
1
2
4
5
7
8
10
public class Code_61 {
	public static void main(String[] args)
	{
		for (int i = 1; i <= 7; i++)
		{
			System.out.println("before continue : " + i);
			if (i % 2 == 0) continue;
			System.out.println("***** after continue : " + i);
		}
	}
}
//결과값
before continue : 1
***** after continue : 1
before continue : 2
before continue : 3
***** after continue : 3
before continue : 4
before continue : 5
***** after continue : 5
before continue : 6
before continue : 7
***** after continue : 7
  • continue 구문은 for, do... while 반복문에서도 사용할 수 있다.

 

  2) 중첩된 반복문

  • 반복문 안에 반복문이 있는 형태를 '중첩된 반복문'이라고 한다.
public class Code_62 {
	public static void main(String[] args)
	{
		int row, col;
		row = 1;
		while (row <= 10)
		{
			col = 1;
			while (col <= 10)
			{
				System.out.print(row * col + " ");
				col ++;
			}
			row ++;
			System.out.println();
		}
	}
}
//결과값
1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 
6 12 18 24 30 36 42 48 54 60 
7 14 21 28 35 42 49 56 63 70 
8 16 24 32 40 48 56 64 72 80 
9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 100
public class Code_63 {
	public static void main(String[] args)
	{
		int row, col;
		for (row = 1; row <= 10 ; row ++)
		{
			for (col = 1; col <= 10; col ++)
			{
				System.out.print(row * col + " ");
			}
			System.out.println();
		}
	}
}
//결과값
1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 
6 12 18 24 30 36 42 48 54 60 
7 14 21 28 35 42 49 56 63 70 
8 16 24 32 40 48 56 64 72 80 
9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 100
728x90
반응형

'Framwork > Java기초' 카테고리의 다른 글

Chapter 6. 메소드  (0) 2022.10.26
Chapter 5. 배열과 문자열  (2) 2022.10.23
Chapter 3. 계산을 위한 연산자  (0) 2022.10.04
Chapter 2. 변수, 자료형, 주석  (1) 2022.09.24
Chapter 1. 자바 시작하기  (6) 2022.09.21