กฎทั่วไป
Semicolon หลัง close brace } จำเป็นต้องมี ถ้าเป็นจุดสิ้นสุดของ declaration (การประกาศ)
กรณีที่ต้องใช้ Semicolon
- Class declaration:
class X {}; - Struct declaration:
struct Y {}; - Enum declaration:
enum Z {}; - Array initialization:
int arr[] = {1, 2};
class X {}; // ต้องมี semicolon
struct Y {}; // ต้องมี semicolon
enum Z {}; // ต้องมี semicolon
int arr[] = {1, 2};
Semicolon เพียงตัวเดียว
Semicolon ด้วยตัวเองคือ empty statement สามารถวางได้ทุกที่ที่ statement ถูกต้อง ดังนั้นการวาง semicolon หลัง brace ของ if/while จึงถูกต้องทาง syntax แต่ไม่เกี่ยวข้องกับ if/while นั้นๆ