พื้นฐาน Embedded C - ตอนที่ 3
C Programming - Control Flow
1. Conditional Statements (if-else)
ทำไมต้องใช้? ตัดสินใจตาม sensor input, user action, states
int16_t temperature = 28;
/* Simple if */
if (temperature > 30)
{
printf("Hot!\r\n");
}
/* if-else */
if (temperature > 30)
{
set_led_color(RED);
}
else
{
set_led_color(GREEN);
}
/* if-else if-else */
if (temperature > 35)
{
printf("Very Hot!\r\n");
set_led_color(RED);
}
else if (temperature > 30)
{
printf("Hot\r\n");
set_led_color(YELLOW);
}
else if (temperature < 15)
{
printf("Cold\r\n");
set_led_color(BLUE);
}
else
{
printf("Normal\r\n");
set_led_color(GREEN);
}Common Patterns:
Ternary Operator:
2. Switch Statement
ทำไมต้องใช้? Multi-way branching, State Machines
Fall-through Pattern:
3. Loops
for Loop:
while Loop:
do-while Loop:
break และ continue:
Infinite Loop (Main Loop):
4. State Machine Pattern
Last updated
Was this helpful?