Loops & Conditional Statements

Conditional Statements

if statement

: An if statement tells the program that it must carry out a specific piece of code if a condition test evaluates to true

if( condition ) {
    statement;
} else{
    statement
}

switch statement

  • a multi-way branch statement.

  • provides an easy way to dispatch execution to different parts of code based on the value of the expression

switch(์ธ์ž๊ฐ’) { //์กฐ๊ฑด์‹์ด ์•„๋‹Œ ์ธ์ž๊ฐ’!

 //-> ์ธ์ž๊ฐ’์—๋Š” int๊ฐ’์œผ๋กœ promotion์ด ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋งŒ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค!

  //ex) ์ •์ˆ˜ํ˜•(byte, short, int), ๋ฌธ์žํ˜•(char)

 

 case ์กฐ๊ฑด๊ฐ’ 1:     // => ์„ธ๋ฏธ์ฝœ๋ก (;) ์•„๋‹ˆ๊ณ  ์ฝœ๋ก (:) ์ž„!!!!!

  ์ˆ˜ํ–‰๋ฌธ; //-> ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ์กฐ๊ฑด๊ฐ’๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ

break;  //=> break๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•จ

 case ์กฐ๊ฑด๊ฐ’ 2:

  ์ˆ˜ํ–‰๋ฌธ;

break;

case ์กฐ๊ฑด๊ฐ’ 3:

  ์ˆ˜ํ–‰๋ฌธ;

break;

case ์กฐ๊ฑด๊ฐ’ 4:

  ์ˆ˜ํ–‰๋ฌธ; 

break;

 Default:  //=> default ๋Š” ์กฐ๊ฑด์ด ํ•˜๋‚˜๋„ ๋งž์ง€ ์•Š์„ ๋•Œ ๋น ์ ธ๋‚˜์˜ค๊ธฐ ์œ„ํ•ด!

  ์ˆ˜ํ–‰๋ฌธ;

Loops

for loop

  • A for loop is divided into three parts, an initialization part, a conditional part and an increment part

  • You should sett all initial values in the initialization part of the loop.

  • A true from the condition part will execute subsequent statements bounded by {} brackets. A false from the condition part will end the loop.

  • For each loop the increment part will be executed.

for (์ดˆ๊ธฐ์‹ ; ์กฐ๊ฑด์‹ ; ์ฆ๊ฐ์‹) {

 ์ˆ˜ํ–‰๋ฌธ 1;

 ์ˆ˜ํ–‰๋ฌธ 2;

 โ€ฆ.

}

๋‹ค์ค‘ for ๋ฌธ

for (์ดˆ๊ธฐ์‹1 ; ์กฐ๊ฑด์‹1 ; ์ฆ๊ฐ์‹1) {

 for (์ดˆ๊ธฐ์‹ 2 ; ์กฐ๊ฑด์‹ 2 ; ์ฆ๊ฐ์‹ 2) {

  ๋ช…๋ น์–ด 2

  }

 ๋ช…๋ น์–ด 1;

}

๋ช…๋ น์–ด 3;

ํ–ฅ์ƒ๋œ for ๋ฌธ (enhanced for loop)

: The enhanced for loop can be used to loop over arrays of any type as well as any kind of Java object that implements the java.lang.Iterable interface.

for ( ํƒ€์ž… ๋ณ€์ˆ˜๋ช… : ๋ฐฐ์—ด or ์ปฌ๋ ‰์…˜) {

 //๋ฐ˜๋ณตํ•  ๋ฌธ์žฅ

}

while ๋ฌธ

: ์„  ๋น„๊ต ํ›„ ์ฒ˜๋ฆฌ

-> ์กฐ๊ฑด์‹์€ ์ƒ๋žต ๋ถˆ๊ฐ€! ์กฐ๊ฑด์‹์ด ํ•ญ์ƒ ์ฐธ์ด ๋˜๊ฒŒ ํ•˜๋ ค๋ฉด true๋ฅผ ๋„ฃ์–ด์•ผํ•จ

while (์กฐ๊ฑด์‹ ) { 

 ์ˆ˜ํ–‰๋ฌธ; -> ์กฐ๊ฑด์‹์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ(true) ์ธ ๋™์•ˆ ๋ฐ˜๋ณต๋  ๋ฌธ์žฅ๋“ค

}

do ~ while ๋ฌธ

: ์กฐ๊ฑด์— ๋งž์ง€ ์•Š์•„๋„ ์ผ๋‹จ 1๋ฒˆ ์‹คํ–‰ ํ•œ๋‹ค

do {

 ์ˆ˜ํ–‰๋ฌธ;

} while (์กฐ๊ฑด์‹); 

break ๋ฌธ

: break๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” loop(๋ฐ˜๋ณต๋ฌธ)๋ฅผ ๋น ์ ธ๋‚˜์˜ค๋Š” ์ œ์–ด๋ฌธ

-> break๋ฌธ์€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค!



for (์ดˆ๊ธฐ์‹ ; ์กฐ๊ฑด์‹ ; ์ฆ๊ฐ์‹) {



 for(์ดˆ๊ธฐ์‹ ; ์กฐ๊ฑด์‹ ; ์ฆ๊ฐ์‹){

  ์ˆ˜ํ–‰๋ฌธ;

  break;   //====>๋‚ด๋ถ€ for๋ฌธ์—์„œ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ ๋‚ด๋ถ€ for๋ฌธ๋งŒ ํƒˆ์ถœํ•œ๋‹ค

 }

}

continue ๋ฌธ

: ์–ด๋Š ํŠน์ • ๋ฌธ์žฅ์ด๋‚˜ ์—ฌ๋Ÿฌ ๋ฌธ์žฅ๋“ค์„ ๊ฑด๋„ˆ๋›ฐ๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ

-> continue ๋ฌธ์„ ๋งŒ๋‚˜๋ฉด continue ์ดํ•˜์˜ ์ˆ˜ํ–‰๋ฌธ๋“ค์€ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ , ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์œ„ํ•ด ์ฆ๊ฐ์‹์œผ๋กœ ๋„˜์–ด๊ฐ (์ฆ๊ฐ์‹์ด ์—†์œผ๋ฉด ์กฐ๊ฑด์‹์œผ๋กœ!)

break ๋ฌธ๊ณผ continue๋ฌธ์˜ ์ฐจ์ด์ 

: ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ€๋Š๋ƒ ๊ทธ๋ ‡์ง€ ์•Š๋Š๋ƒ!

-> continue๋ฌธ์€ ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ€์ง€ ์•Š๊ณ , ๋‹ค์Œ ๋ฐ˜๋ณต ํšŒ์ฐจ ์ˆ˜ํ–‰์„ ์œ„ํ•ด ๋ฐ˜๋ณต๋ฌธ์˜ ์กฐ๊ฑด์‹์œผ๋กœ ๋„˜์–ด๊ฐ!

return๋ฌธ์€ function ์ž์ฒด๋ฅผ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค

Last updated