ตั้งค่าการอ่าน

ค่าเริ่มต้น

  • เลื่อนอัตโนมัติ
    Java

    ลำดับตอนที่ #5 : Java II - Control Flow

    • อัปเดตล่าสุด 28 ธ.ค. 61


    if Statements

    Control Flow

              In daily life, we make decisions based on conditions. A simple example would be, "If it's raining, bring an umbrella". In programming, there are also times when we want to do different things based on certain conditions. This is known as control flow.

    if Statements

              The if statement is the most common type of control flow. By putting a condition in the () after if, the code inside the {} is only executed when the condition is true.

    Using if Statements

              Let's take a look at an example of an if statement. Booleans, comparison operators (==, <) and logical operators (&&, ||, !) are often used in if statement conditions.

    Blocks

              { } in an if statement is called block. It helps to put multiple lines of code together. All the lines within a block get executed when the condition is true (semicolons are unnecessary after {}).


    else, else if

    else

              Using the else statement, you can add some code you want to run when the condition of the if statement is false. Look at the example below to learn its syntax.

    else if

              You can use the else if statement to add alternative conditions to the control flow. Check the two examples below!

    When Multiple Conditions Match

              You can add as many else if statements as you want. However, keep in mind that only the code that returns true first will be executed.

     


    Switch Statements

              The switch statement is another type of control flow. It has many cases, and the case that matches the value of the condition gets executed. Don't forget to put colons : after each case. No need to worry about the break yet!

    If Statements and Switch Statements

              Let's compare switch statements to if statements. The if statement adds flow based on conditions, whereas the switch statement adds flow based on values. Switch statements are better for when there are a lot of conditions based on values.


    Break Statements

              With switch statements, every case needs a break statement at the end. Without it, the switch statement would not end, and continue on to the next case. Make sure to put it for every case!

    The Default Case

              In switch statements, you can set a default case for when none of the cases match. It functions similarly to an else in if statements.


    Indents

              Indents are the spaces you add at the beginning of a line. In the left example, it's hard to see which line belongs to which case. By indenting appropriately, the structure of the code will be clear. We recommend you add 2 spaces for an indent.








    ติดตามเรื่องนี้
    เก็บเข้าคอลเล็กชัน
    นิยายแฟร์ 2024

    ผู้อ่านนิยมอ่านต่อ ดูทั้งหมด

    loading
    กำลังโหลด...

    อีบุ๊ก ดูทั้งหมด

    loading
    กำลังโหลด...

    ความคิดเห็น

    ×