> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/c-c++-for-embedded-programming/principle-c-c++-programming/if-and-if-else.md).

# If and If else

เมื่อเราเขียนโปรแกรมโดยที่เราต้องการให้โปรแกรมทำตามคำสั่งโดยเงื่อนไขบางอย่าง\
เราสามารถกำหนดเงื่อนไขนั้นๆ ได้โดยการใช้คำสั่ง if โดยหลักการของคำสั่งจะเป็นไปตามด้านตัวอย่างแบบต่างๆด้านล่างดังนี้

### การเขียนโปรแกรมสร้างเงื่อนไขด้วย if

Syntax การเขียน if statement นั้นจะเป็นดังนี้

{% hint style="success" %}
if (condition) {

&#x20;   things you want to do;

}
{% endhint %}

```cpp
#include<iostream>
using namespace std;

int main (){
    int A = 0;
    cout<<"Please enter a number to A: "<< endl;
    cin >> A;

    if(A<5){
        cout<<"Condition met"<<endl;
    }
    
    return 0;
}
```

นอกจากนั้นเราสามารถใส่เงื่อนไขได้มากกว่า 1 เงื่อนไขได้ในหนึ่ง condition

```cpp
#include<iostream>
using namespace std;

int main () {
    int A = 0;
    cout<<"Please enter a number to A: "<< endl;
    cin >> A;

    if(A<5 && A >1){ // two condition need to met
        cout<<"Condition met"<<endl;
    }
    
    return 0;
}
```

หรือเราสามารถใส่เงื่อนไขหลายๆข้อได้โดยการนำ if มาต่อกัน

```cpp
#include<iostream>
using namespace std;

int main () {
    int A = 0;
    cout<<"Please enter a number to A: "<< endl;
    cin >> A;

    if(A<8) {
        cout<<"A less than 8"<<endl;
    }
    if(A<6) {
        cout<<"A less than 6"<<endl;
    }
    if(A<4) {
        cout<<"A less than 4"<<endl;
    }
    if(A<7) {
        cout<<"A less than 7"<<endl;
    }
    if(A<5) {
        cout<<"A less than 5"<<endl;
    }
    
    return 0;
}
```

เมื่อรันแล้วจะเห็นได้ว่าทั้งสามรูปแบบต่างมีเอกลักษณ์ของตัวมันเองซึ่งอยู่กับสถานะการณืของการออกแบบของผู้ใช้ว่าแบบไหนจะเหมาะสมที่สุดสำหรับการนำไปใช้เขียนโปรแกรมที่ต้องการ

### การเขียนโปรแกรมสร้างเงื่อนไขด้วย if else

Syntax การเขียน if else statement นั้นจะเป็นดังนี้ \
โดยการทำงานของมันจะทำสองอย่างคือ เมื่อเป็นไปตามเงื่อนไขจะทำคำสั่งที่อยู่ภายใต้วงเล็บของ if และถ้าไม่เป็นไปตามเงื่อนไขจะไปทำงานอยู่ภายใต้วงเล็บของ else

{% hint style="info" %}
if (condition) {

&#x20;   things you want to do;

}&#x20;

else {

&#x20;   things you want to do if it not met the condition;

}
{% endhint %}

```cpp
#include<iostream>
using namespace std;

int main () {
    int A = 0;
    cout<<"Please enter a number to A: "<< endl;
    cin >> A;

    if(A == 8) {
        cout<<"A equal 8"<<endl;
    }
    else {
        cout<<"A is other number than 8"<<endl;
    }
    
    
    return 0;
}
```

เรายังสามารถกำหนดเงื่อนไขอื่นๆได้มากกว่า 1 เงื่อนไขเช่นเดียวกับการเขียนเงื่อนไขแบบ if ธรรมดา

```cpp
#include<iostream>
using namespace std;

int main () {
    int A = 0;
    cout<<"Please enter a number to A: "<< endl;
    cin >> A;

    if(A < 4) {
        cout<<"A less than 4"<<endl;
    }
    else if(A < 3) {
        cout<<"A less than 3"<<endl;
    }
    else if(A < 7) {
        cout<<"A less than 7"<<endl;
    }
    else if(A < 8) {
        cout<<"A less than 8"<<endl;
    }
    else if(A < 9) {
        cout<<"A less than 9"<<endl;
    }
    else {
        cout<<"A is 9 or larger"<<endl;
    }
    
    
    return 0;
}
```

จากการรัน code ตัวอย่างด้านจะเห็นว่าเมื่อไหร่ก็ตามที่ A เข้าเงื่อนไขใดเงื่อนไขหนึ่งแล้วเป็นจริง จะไม่ทำการตรวจสอบเงื่อนไขอื่นๆต่อ นี่คือเอกลักษณ์ของการใช้ if else แต่อย่างไรก็ตามผู้เขียนจำเป็นต้องระมัดระวังในการเขียนเงื่อนไขที่ไม่จำเป็น

{% hint style="info" %}
จากตัวอย่างด้านบน ผู้ศึกษาสามารถตอบได้หรือไม่ว่าเงื่อนไขใดที่ไม่จำเป็นในโปรแกรม?
{% endhint %}

## แหล่งอ้างอิง :

<https://www.w3schools.com/cpp/cpp_conditions.asp>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/principle-c-c++-programming/if-and-if-else.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
