everything in C++ is about statements (fragments of the C++ program that are executed in sequence). if you read about statements, it becomes clear:
- An expression statement is an expression followed by a semicolon
- A compound statement or block groups a sequence of statements into a single statement by surrounding the sequence of statements with {}
so when you want to assign a value to a variable, you have a simple expression statement and thus you do int x = 3 and you end with the semi colon to denote the end of that statement. so int x = 3;
if x was an array and you want to provide an Aggregate initialization ( a form of list-initialization or direct initialization) then the value is within brackets and the terminating semi colon is there to mark the end of the expression statement.
now you have statements all over the place, for example an if() is a Selection statements and it's syntax is if ( condition ) statement ➜ so what you execute when the if is true is a statement. if it's a simple expression statement like above, you don't even need brackets
if (condition) x = 3; ➜ the statement is x=3;
now you can decide to put a compound statement there and so use the brackets
if (condition) {...} ➜ no need for the semicolon since the compound statement definition says you don't need it.