ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Conditional
    Java 2022. 2. 5. 01:22

    자바의 조건문에 대해 정리하였습니다.

     

     


    < Introduction >

    To make decisions based on circumstances.
    This means To check a specific condition(s) and perform a task based on the condition(s)

     

     


    < If Statement >

    < Syntax >
    if (condition) {
    	// code block
    }
    • Condition evaluates to true, block runs.
    • Condition evaluates to false, block won't run.

    Note: Conditional statements do not end in a semicolon.

     

     

    Example)

    if (number > 12) {
      System.out.println("Number is greater than 12");
    }

     

    +. If a conditional is brief we can omit the curly braces entirely:

    if (true) System.out.println("Brief Sentence");

     

     


    If-Else Statement >

    < Syntax >
    if (condition) {
      // code block-1
    } else {
      // code block-2
    }
    • Condition evaluates to true, code block-1 runs.
    • Condition evaluates to false, code block-2 runs.

     

     


    If-Else if Statement >

    < Syntax >
    if (condition) {
      // code block-1
    } else if (condition) {
      // code block-2
    } else if (condition) {
      // code block-3
    } ...
    • The first condition to evaluate to true will have that code block run.
    • Only one of the code blocks will run.

     

    Example)

    if (Number > 10) {
      System.out.println("Number is greater than 10");
    } else if (Number > 20) {
      System.out.println("Number is greater than 20");
    } else if (Number > 30) {
      System.out.println("Number is greater than 30");
    } else {
      System.out.println("Input Number");
    }

     

     


    Nested Conditional Statements >

    We can create more complex conditional structures by creating nested coditional statements.

     

    Example)

    int temp = 45;
    boolean raining = true;
     
    if (temp < 60) {
      System.out.println("Wear a jacket!");
      if (raining == true) {
        System.out.println("Bring your umbrella.");
      } else {
        System.out.println("Leave your umbrella home.");
      }
    }

     

     


    < Switch Statement >

    An else if 's another version. It's easier to read and write!

     

    < Syntax >
    switch (expression) {
      case value1:
        // code block-1
        break;
      case value2:
        // code block-2
        break;
      
      //...
      default:
        // default code block
    }
    • If the expression matches the specified value, that case's code block will run.
    • The break keyword tells the computer to exit the block and not execute any more code.
      Note : Without break keywords, every subsequent case will run regardless of wheter or not it matches!
    • If none of the cases are true, then the code in the default statement will run.

     

    Example)

    String course = "Biology";
     
    switch (course) {
      case "Algebra": 
        // Enroll in Algebra
      case "Biology": 
        // Enroll in Biology
      case "History": 
        // Enroll in History
      case "Theatre":
        // Enroll in Theatre
      default:
        System.out.println("Course not found");
    }

     

     

     

     

    'Java' 카테고리의 다른 글

    Array  (0) 2022.02.05
    Conditional Operators  (0) 2022.02.05
    Class : Method  (0) 2022.02.04
    Class : Introduction  (0) 2022.02.03
    Variables : Manipulating  (0) 2022.02.01

    댓글

Designed by Tistory.