为什么不能在else循环中放置else-if语句?

可能是一个超级愚蠢的问题,但这是我的代码:

if (currentItem.Name == "MicrometerTags.index")
{
    histIndex = Convert.ToInt32(currentValue);
}

for (int k = 1; k <= 20; k++)
{
    else if (currentItem.Name == "MicrometerTags.meas" + k)
    {
        Properties.Settings.Default["meas" + k] = currentValue;
    }
}

Visual Studio告诉我在{for循环下的第一个有错误,因为它期望一个}但是您可以在那看到我的右括号吗?我在想else-if语句在这里做错了什么。

编辑:好的,我不知道您无法做到这一点。那么,还有一种更好的方法来检查是否currentItem等于我的20个度量之一,而不是手动写出20个if语句?

EDIT2:这是我想要制作的代码。感谢BlueMonkMN提供的解决方案。

            if (currentItem.Name == "MicrometerTags.index")
            {
                histIndex = Convert.ToInt32(currentValue);
            }

            else for (int k = 1; k <= 20; k++)
            {
                if (currentItem.Name == "MicrometerTags.meas" + k)
                {
                    Properties.Settings.Default["meas" + k] = currentValue;
                }
            }
耶利奇

if语句后的右括号有效地关闭了if语句,此时不再有活动的“ if块”

if (something == true)
{  // open the if block

} // close the if block

// no more if block in action here...

为了使别的东西起作用,它需要紧跟在该结束括号之后:

if (something == true)
{  // open the if block

} // close the if block

else // next statement must be else for this to work
{  // open else block

}  // close else block

通常,这是用以下语法编码的:

if (someCondition) {
    // if true do this
} else {
    // otherwise do this
}

请注意,else仍然是if块右括号之后的下一个语句。

您无法使用的原因是,在结束if括号和else语句之间还有其他语句。从逻辑上讲,我不确定您要做什么,但是从语法角度来看,以下内容是合法的:

if (someCondition == true) {
    // do stuff 
} else {
    for(int i = 0; i < someCount; i++) {
        // do stuff
    }   
}

for (int i = 0; i < someCount; i++) {

    if (someCondition == true) {
        // do stuff
    } else {
        // do other stuff
    }

}

注意打开和关闭if和for块的顺序。如果IF首先打开,则必须最后关闭。如果for首先打开,则必须最后关闭(aka,先进先出模式)。这种模式对于任何一种编程语言都是基本的,并且不仅限于ifs和fors,而且还限于任何块级编程(甚至是名称空间,类,方法等)。

这也适用于嵌套逻辑:

if (condition1 == true) {

    if (condtion2 == true) 
    {

        // stuff

    }   // end condition2's if block 
    else 
    {

        // stuff

    }   // end condition2's else block


} // end condition 1's if block
else 
{

    // other stuff

}   // end condition 1's else block

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我不能在类中放置 switch 语句

为什么不能在 if....else 中使用 str 语句

在 for 循环中循环 else 语句?

为什么“ if else语句”不能正常工作?

为什么 javascript 'if else' 语句似乎在循环

为什么我们不能在插入排序的while循环中更改语句的顺序?

为什么我的for循环中的if语句不能在Java脚本中运行

为什么我不能在 Java 中使用像 if-else 语句这样的 (? :) 运算符?

为什么“ if”语句不能在while循环中运行,而while循环中也没有其他事件发生?

为什么即使不满足“if”语句的条件,“else”语句也没有在我的循环中执行?

尝试在 For 循环中运行 Else 语句

何时需要循环中的 else 语句

Else 语句无法在 while 循环中工作

vba-for循环中的if else语句

如何避免for循环中的else语句?

在循环中嵌套if和else语句

将 else 语句放入循环中

为什么python代码不能到达else语句?

为什么我的 else/if 语句在 javascript 中不能正常工作?

在这里,对于非素数,执行内循环中的break语句,但不执行外循环中的else语句,为什么?

为什么while循环在else语句之前停止?

If / Else语句破坏循环

循环“ If Else语句” java

为什么 if 语句在 while 循环中阻塞?

为什么我不能在此处放置打印功能语句?

if/else 语句

Java 不能在 if/else 语句中返回变量

为什么我的“为什么”循环中的“如果”语句不能更改我的一个对象?

为什么我的IF ELSE语句向后默认?