跳轉到內容

C# 入門/決策

來自華夏公益教科書,開放的書,為一個開放的世界

If 語句

[編輯 | 編輯原始碼]

C# 中的 if 語句與其他語言一樣簡單。if 語句的語法如下

if(condition){
// Statements
}

這裡我們可以檢查一些表示式是否為真或假。例如 5 > 4 為真,但 4 > 5 為假。

if(5 > 4){
Console.WriteLine("Yes five is still greater than 4");
}

Else 部分

[編輯 | 編輯原始碼]

在 else 部分中,如果 if 條件為假,我們可以給出將執行的語句。

if(5 > 4){
Console.WriteLine("Yes five is still greater than four");
} else {
Console.WriteLine("Oh No! five is now no longer greater than four");
}
華夏公益教科書