簡體   English   中英

關於if-else后面的else語句的問題

[英]Question about else statement following if-else

我正在復習一些 C# 並且有一個看起來很愚蠢的問題。 在 C#(可能還有大多數語言)中,您可以將 else 語句放在 if-else 語句中嗎?

例如

if (clause) {
  execute code
}
else if (clause) {
  execute code
     else {
       execute code
  }
}

這應該是錯誤的。 你可能想要這樣的東西:

if (clause)
{
    // do something
}
else
{
    if (anotherClause)
    {
        // do something
    }
    else
    {
        // do something
    }
}

我假設你是編程新手,所以我可以談談 if else 語句。

如果前面沒有ifelse就沒有任何意義。

例如,這句話是有道理的:

IF you are 21 or older, you can drink. ELSE, you cannot drink.

雖然這不會:

ELSE, you cannot drink

希望我的回答有幫助

該代碼不起作用,因為您需要一個if才能有一個else 如果您想對else if中的某些內容進行額外檢查,則需要先添加一個if ,如下所示:

if (clause)
{
  execute code
}
else if (clause)
{
  execute code
    if (clause)
    {
       execute code
    } 
    else
    {
       execute code
    }
}

這是不可能的,但你可以做類似的事情:

if(condition)
{
  //code that get executed after cheking the condition 
}
else if(another condition)
{
  // code that get executed after checking the second condition
}
else
{
 // code get executed if the first and the second condition are not true
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM