if / else

์ฃผ์–ด์ง„ boolean ๊ฐ’์— ๋”ฐ๋ผ ๋ช…๋ น๋ฌธ ์‹คํ–‰ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •

package sec04.chap01;

public class Ex01 {
    public static void main(String[] args) {
        boolean open = true;
        int saleFrom = 10, saleTo = 20;
        int today = 15;

        //  ๐Ÿ’ก if : ๊ด„ํ˜ธ ์•ˆ์˜ boolean ๊ฐ’์ด true๋ฉด ๋‹ค์Œ ๋ช…๋ น ์‹คํ–‰
        if (open) System.out.println("์˜์—…์ค‘");
        if (!open) System.out.println("์˜์—…์ข…๋ฃŒ");

        //  ๐Ÿ’ก ์‹คํ–‰ํ•  ๋ช…๋ น์ด ํ•œ ์ค„ ์ด์ƒ์ผ ๊ฒฝ์šฐ ๋ธ”๋ก ์‚ฌ์šฉ
        if (today >= saleFrom && today <= saleTo) {
            System.out.println("์„ธ์ผ์ค‘์ž…๋‹ˆ๋‹ค.");
            System.out.println("์ „ํ’ˆ๋ชฉ 20% ํ• ์ธ");
        }
    }
}
	//  ๐Ÿ’ก else : if๋ฌธ ์•ˆ์˜ boolean ๊ฐ’์ด false์ผ ๊ฒฝ์šฐ ์‹คํ–‰
        if (open) {
            System.out.println("์˜์—…์ค‘");
        } else {
            System.out.println("์˜์—…์ข…๋ฃŒ");
        }

package sec04.chap01;

public class Ex02 {
    public static void main(String[] args) {
        boolean willOrderCoffee = true;
        boolean likeMilk = false;
        boolean likeIce = true;
        boolean likeSweet = false;
        boolean angry = false;
        int myRank = 2;

        //  ๐Ÿ’ก ์ค‘์ฒฉ ์‚ฌ์šฉ
        if (willOrderCoffee) {

            //  โญ๏ธ ๋ธ”๋ก ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๋ณ€์ˆ˜ (์ดํ›„ ์Šค์ฝ”ํ”„์—์„œ ๋ฐฐ์›€)
            String toOrder = "";

            //  ํ•„์š”์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๊ฒƒ ์‚ฌ์šฉ
            toOrder = likeMilk ? "๋ผ๋–ผ" : "์•„๋ฉ”๋ฆฌ์นด๋…ธ";
            if (likeIce) toOrder = "์•„์ด์Šค " + toOrder;
            if (!likeSweet) toOrder += " ์‹œ๋Ÿฝ์ถ”๊ฐ€";

            System.out.printf("์ €๋Š” %s ํ• ๊ฒŒ์š”%n", toOrder);
        } else {
            if (!angry || myRank > 3) {
                System.out.println("์ €๋Š” ๊ดœ์ฐฎ์•„์š”.");
            } else {
                System.out.println("๋„ˆ๋‹˜๋“ค๋ผ๋ฆฌ ๋“œ์„ธ์š”.");
            }
        }

    }
}

package sec04.chap01;

public class Ex03 {
    public static void main(String[] args) {
        int score = 85;

        //  ๐Ÿ’ก else if : ์ฒซ if๋ฌธ์ด false์ผ ๋•Œ ๋‹ค๋ฅธ ์กฐ๊ฑด์„ ์—ฐ์† ์‚ฌ์šฉ
        //  else๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋งจ ๋งˆ์ง€๋ง‰์—
        if (score > 90) {
            System.out.println('A');
        } else if (score > 80) {
            System.out.println('B');
        } else if (score > 70) {
            System.out.println('C');
        } else if (score > 60) {
            System.out.println('D');
        } else {
            System.out.println('F');
        }
        
    }
}

package sec04.chap01;

public class Ex04 {
    public static void main(String[] args) {
        int score = 85;

        //  โญ ๋ณด๋‹ค ๊ฐ€๋…์„ฑ ์ข‹์€ ๋ฐฉ์‹
        //  return๋ฌธ: ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ๋น ์ ธ๋‚˜์˜ด

        if (score > 90) {
            System.out.println('A');
            return;
        }
        if (score > 80) {
            System.out.println('B');
            return;
        }
        if (score > 70) {
            System.out.println('C');
            return;
        }
        if (score > 60) {
            System.out.println('D');
            return;
        }
        System.out.println('F');
    }
}

Last updated