Skip to content

Commit fa37f4e

Browse files
CopilotBillWagner
andcommitted
Add definitions for expressions and statements to C# fundamentals program structure
Co-authored-by: BillWagner <[email protected]>
1 parent b70334e commit fa37f4e

File tree

1 file changed

+31
-0
lines changed
  • docs/csharp/fundamentals/program-structure

1 file changed

+31
-0
lines changed

docs/csharp/fundamentals/program-structure/index.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ You can also create a static method named [`Main`](main-command-line.md) as the
1919

2020
In that case the program will start in the first line of `Main` method, which is `Console.WriteLine("Hello world!");`
2121

22+
## Expressions and statements
23+
24+
Understanding the distinction between expressions and statements is fundamental to C# programming:
25+
26+
### Expressions
27+
28+
An **expression** is a combination of values, variables, operators, and method calls that evaluates to a single value. Expressions produce a result and can be used wherever a value is expected.
29+
30+
Examples of expressions:
31+
32+
- `42` (literal value)
33+
- `x + y` (arithmetic operation)
34+
- `Math.Max(a, b)` (method call)
35+
- `condition ? trueValue : falseValue` (conditional expression)
36+
- `new Person("John")` (object creation)
37+
38+
### Statements
39+
40+
A **statement** is a complete instruction that performs an action. Statements don't return values; instead, they control program flow, declare variables, or perform operations.
41+
42+
Examples of statements:
43+
44+
- `int x = 42;` (declaration statement)
45+
- `Console.WriteLine("Hello");` (expression statement - wraps a method call expression)
46+
- `if (condition) { /* code */ }` (selection statement)
47+
- `return result;` (jump statement)
48+
49+
The key distinction: expressions evaluate to values, while statements perform actions. Some constructs, like method calls, can be both—`Math.Max(a, b)` is an expression when used in `int result = Math.Max(a, b);`, but becomes an expression statement when written alone as `Math.Max(a, b);`.
50+
51+
For detailed information about statements, see [Statements](../../programming-guide/statements-expressions-operators/statements.md). For information about expression-bodied members and other expression features, see [Expression-bodied members](../../programming-guide/statements-expressions-operators/expression-bodied-members.md).
52+
2253
## Related Sections
2354

2455
You learn about these program elements in the [types](../types/index.md) section of the fundamentals guide:

0 commit comments

Comments
 (0)