๐Ÿ”ฅ 4์ฃผ์ฐจ ๊ณผ์ œ: ์ œ์–ด๋ฌธ

glowing713ยท2020๋…„ 12์›” 11์ผ
0

live-java-study

๋ชฉ๋ก ๋ณด๊ธฐ
1/2
post-thumbnail
  • ๋ฐฑ๊ธฐ์„  ๋‹˜์ด ์œ ํŠญ์—์„œ ์ง„ํ–‰ํ•˜๋Š” JAVA Live-study ๊ณผ์ •์„ ๊ธฐ๋กํ•œ๋‹ค.
  • ๋งค ์ฃผ ์ด์Šˆ์— ์˜ฌ๋ผ์˜จ ์งˆ๋ฌธ๋“ค์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๊ณ  ๋‹ต์„ ๋‚จ๊ธฐ๊ณ , ์ด์Šˆ์— ๋งํฌ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค. ์ถ”ํ›„ ๋ฐฑ๊ธฐ์„  ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๋ผ์ด๋ธŒ๋ฅผ ํ†ตํ•ด ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

์ฆ‰, Java ๊ณต๋ถ€๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ณผ์ •์ด๋‹ค ๐ŸŒฑ

๐Ÿ“Œ ๋ชฉํ‘œ

์ž๋ฐ”๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ œ์–ด๋ฌธ์„ ํ•™์Šตํ•˜์„ธ์š”.

๐Ÿ“Œ ํ•™์Šตํ•  ๊ฒƒ (ํ•„์ˆ˜)

  • ์„ ํƒ๋ฌธ
  • ๋ฐ˜๋ณต๋ฌธ

์ œ์–ด๋ฌธ์ด๋ž€?

์ œ์–ด๋ฌธ(Control-flow statement)์€ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. Java์˜ ์ œ์–ด๋ฌธ์€ decision making, branching, looping, conditional block์ด ์žˆ๋‹ค.

(์ด๋ฏธ์ง€ ์ถœ์ฒ˜: https://soshace.com/wp-content/uploads/2020/01/untitled-drawing.jpg)


Decision making statements(์„ ํƒ๋ฌธ)

  • if statement
    if ๋ฌธ์€ expression๊ณผ statement๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. expression์ด ์ฐธ์ด ๋˜๋Š” ๊ฒฝ์šฐ, ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ statement๋ฅผ ์‹คํ–‰ํ•˜๊ณ , expression์ด ๊ฑฐ์ง“์ด ๋˜๋Š” ๊ฒฝ์šฐ, ๊ทธ statement๋ฅผ ์Šคํ‚ตํ•œ๋‹ค.
if (username == null)	// If username is null,
    username = "John Doe";	// use a default value

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

  • if-else statement
    if ๋ฌธ์€ ๋˜ํ•œ ์„ ํƒ์ ์œผ๋กœ else ํ‚ค์›Œ๋“œ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. expression์ด ์ฐธ์ด๋ผ๋ฉด ์ฒซ ๋ฒˆ์งธ statement๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๊ฑฐ์ง“์ด๋ผ๋ฉด ๋‘ ๋ฒˆ์งธ statement๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.
if (username != null)
    System.out.println("Hello " + username);
else {
    username = askQuestion("What is your name?");
    System.out.println("Hello " + username + ". Welcome!");
}

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

  • else-if clause
    ๋งŒ์•ฝ, ์—ฌ๋Ÿฌ ๋ธ”๋ก์˜ ์ฝ”๋“œ ์ค‘ ์„ ํƒํ•ด์•ผํ•œ๋‹ค๋ฉด else-if ์ ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค.
if (n == 1) {
    // Execute code block #1
}
else if (n == 2) {
    // Execute code block #2
}
else if (n == 3) {
    // Execute code block #3
}
else {
    // If all else fails, execute code block #4
}

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

  • switch statement
    ๋งŒ์•ฝ, ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜ ๊ฐ’์— ๋”ฐ๋ผ ์„ ํƒ์ง€๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค๋ฉด, ์—ฌ๋Ÿฌ ๊ฐœ์˜ if ๊ตฌ๋ฌธ์„ ๋Œ€์‹ ํ•˜์—ฌ switch ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
switch(n) {
    case 1:			// Start here if n == 1
      // Execute code block #1
      break;			// Stop here
    case 2:			// Start here if n == 2
      // Execute code block #2
      break;			// Stop here
    case 3:			// Start here if n == 3
      // Execute code block #3
      break;			// Stop here
	default:		// If all else fails...
      // Execute code block #4
      break;			// Stop here
}

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

Looping statements(๋ฐ˜๋ณต๋ฌธ)

  • while statement
    while ๋ฌธ์€ Java์˜ ๊ธฐ๋ณธ์ ์ธ ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.
    expression์˜ ์ฐธ/๊ฑฐ์ง“์„ ํŒ๋ณ„ํ•˜์—ฌ(boolean ๋˜๋Š” Boolean์ด ๊ฒฐ๊ณผ๊ฐ’์ธ ์ž‘์—…) ๊ฑฐ์ง“์ด๋ผ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๊ณ  ๋‹ค์Œ ๊ตฌ๋ฌธ์œผ๋กœ ์ด๋™ํ•œ๋‹ค. ์ฐธ์ด๋ผ๋ฉด ๋ฐ˜๋ณต์ด ์žฌ๊ฐœ๋˜๋ฉฐ expression์˜ ์ฐธ/๊ฑฐ์ง“์ด ๋‹ค์‹œ ํ‰๊ฐ€๋œ๋‹ค.
/*
   ๋ณ€์ˆ˜ count๋Š” 0์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. while ๋ฌธ์€ 10๋ฒˆ ์‹คํ–‰๋˜๊ณ ,
   count๊ฐ€ 10์ด ๋˜์–ด ์กฐ๊ฑด์ธ count < 10 ์ด false๊ฐ€ ๋˜๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋œ๋‹ค.
*/
int count = 0;
while (count < 10) {
    System.out.println(count); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    count++;
}

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

  • do statement
    do ๋˜๋Š” do-while ๋ฌธ์€ while ๋ฌธ๊ณผ ๋น„์Šทํ•˜๋‹ค. while ๋ฌธ์€ ์ฐธ/๊ฑฐ์ง“์„ ํŒ๋ณ„ํ•˜๋Š” expression์„ ๋จผ์ € ๋ณด์ง€๋งŒ do-while ๋ฌธ์€ loop์ด ๋จผ์ € ์‹คํ–‰๋˜๊ณ  expression์„ ํŒ๋ณ„ํ•œ๋‹ค. ์ฆ‰, do-while ๋ฌธ์€ ์ตœ์†Œ ํ•œ ๋ฒˆ ์ด์ƒ loop๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
int count = 0;
do {
   System.out.println(count);
   count++;
} while(count < 10);

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

  • for statement
    ๋Œ€๋ถ€๋ถ„์˜ ๋ฐ˜๋ณต ๊ตฌ๋ฌธ์€ counter๋‚˜ state๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์กด์žฌํ•˜๊ณ  loop ๊ณผ์ •๋™์•ˆ ์ฐธ/๊ฑฐ์ง“์ด ํŒ๋ณ„๋œ๋‹ค. for ๋ฌธ์€ ์ด๋Ÿฌํ•œ ๋ณ€์ˆ˜์˜ initialize, update ๊ณผ์ •์„ ๋”์šฑ ๋ช…๋ฐฑํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฌธ๋ฒ• ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
// while ๋ฌธ์˜ initialize, update ๊ณผ์ •์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด,
initialize;
while (test) {
    statement;
    update;
}
// for ๋ฌธ์˜ initialize, update ๊ณผ์ •์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
for (initialize; test; update) {
    statement
}
/*
   ์œ„์—์„œ while ๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ for ๋ฌธ ํ˜•์‹์œผ๋กœ
   ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ์˜ˆ์ œ์ด๋‹ค.
*/
for (int count = 0; count < 10; count++) {
    System.out.println(count);// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
/*
   comma(,)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ multiple initialization๊ณผ update๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
*/
for (int i = 0, j = 10; i < 10; i++, j--) {
    sum += i * j;
}
/*
   for ๋ฌธ์€ ๊ณ„์ˆ˜(counting numbers)์—๋งŒ ๊ตญํ•œ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค.
   ์˜ˆ๋ฅผ ๋“ค์–ด, ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ(Linked List)์—์„œ iterate๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
*/
for (Node n = listHead; n != null; n = n.nextNode()) {
    process(n);
}
/*
   for ๋ฌธ์˜ initialize, update, test ๊ตฌ๋ฌธ์€ ๊ฐ๊ฐ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.
*/
for (;;) {
    // ์ด๋ ‡๊ฒŒ ์‹คํ–‰ํ•˜๋ฉด ํ•ญ์ƒ ์ฐธ์ด ๋˜์–ด ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋œ๋‹ค.
}

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

  • foreach statement
    foreach ๋ฌธ์€ Object๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ์— ์žˆ์–ด ๋ถˆํ•„์š”ํ•˜๊ฒŒ ํˆฌ๋ฐ•ํ•œ ๋ถ€๋ถ„๋“ค์„ ์ œ๊ฑฐํ•œ ๊ตฌ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ foreach ๊ตฌ๋ฌธ์ด๋ผ๊ณ  ํ•ด์„œ each๋ผ๋Š” ํ‚ค์›Œ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค. ์œ„ ๋ฐ˜๋ณต๋ฌธ๋“ค์—์„œ 10๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ iterateํ•  ์ˆ˜ ์žˆ๋Š” ์ปฌ๋ ‰์…˜์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ด ์ฐจ์ด์ ์ด๋‹ค.
// These are the numbers we wnat to print
int[] primes = new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
// This is the loop that prints them
for (int n : primes)
    System.out.println(n);

(์†Œ์Šค์ฝ”๋“œ ์ถœ์ฒ˜: O'Reilly, <Java in a Nutshell 7th edition>)

(์ถœ์ฒ˜: https://soshace.com/guide-to-control-flow-statements-in-java/)

0๊ฐœ์˜ ๋Œ“๊ธ€