Luồng điều khiển chương trình được chỉ định bởi các câu lệnh điều khiển (control statements).
1. if statement
if (boolean_expression)
{
// Thực thi nếu true
}
2. if-else statement
if (boolean_expression)
{
// true
}
else
{
// false
}
3. for loop
Thực thi chuỗi câu lệnh nhiều lần, quản lý biến lặp ngắn gọn.
for (init; condition; increment)
{
statement(s);
}
4. while loop
Lặp lại khi điều kiện true. Kiểm tra điều kiện trước khi vào body.
while (condition)
{
statement(s);
}
5. do...while loop
Tương tự while nhưng kiểm tra điều kiện sau body (chạy ít nhất 1 lần).
do
{
statement(s);
} while (condition);
6. foreach loop
Duyệt tập hợp (IEnumerable) không cần index.
foreach (var item in collection)
{
statement(s);
}
7. jump statements
break: Thoát loop/switch.continue: Bỏ qua vòng lặp hiện tại, tiếp tục vòng sau.return: Thoát method.goto: Nhảy đến label (tránh dùng).throw: Ném exception.