是否存在else if语句? [英] Does an else if statement exist?

查看:181
本文介绍了是否存在else if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间没有站在这样的行之后:

  if(arg)
invk(test) ;
else if(test)
{
alot();
stuff();
}

我决定自己在1920x1200次的可读性方面做得更好,不是省略 {}



所以我编写了一个重新格式化现有代码的工具。



后来我注意到该工具中的一个错误导致

  if(x)
{
...
}
else if(y)
{
...
}
else if(z)
{
...
}

已被更改(不包括如果(x)
{
... $ b $,b / b

b}
else
{
if(y)
{
...
}
else
{
如果(z)
{
...
}
}
}

这让我意识到(无意)这实际上是一个否则,如果通过C的语法和语义规则来做。



因此,如果() existi,甚至会出现类似的声明或者只是滥用语义导致这个有用但是(为了这个目的,我们称之为)混淆起源的措辞打破了任何格式化规则并且只是作为人类可读的?

解决方案

根据 C11 ,章节§6.8.4,选择陈述,可用语句块是

  if(表达式)语句
if(表达式)语句else语句
switch(表达式) )声明

,关于 else 部分,


else 与词汇最接近的 if ,这是
语法所允许的。


所以,没有否则,如果构造,则按照标准 C 存在。



显然,如果(或 if ... else )块可以作为 else 块中的语句存在(嵌套 if ... else ,我们可以说)。



缩进的选择留给用户。






注意:



只需添加,就有如果构造存在,则没有理由单独的 else,可以通过嵌套来实现功能。请参阅此Wiki文章。相关报价


经常遇到的否则 C 系列语言中,在COBOL和Haskell中,不是语言功能,而是一组嵌套且独立的if then else语句结合特定的源代码布局。但是,这也意味着在这些语言中不需要明确的 else-if 构造。



Some time ago after not standing anymore lines like this:

if (arg)
    invk(test);
else if (test)
{
    alot();
    stuff();
}

I decided for my self its better for readability in our 1920x1200 times, to not omit the {}.

So I wrote a tool that reformats my existing code.

later I noticed a bug in that tool resulting in

if (x)
{
 ...
}
else if(y)
{
 ...
}
else if(z)
{
 ...
}

had been changed (wihtout obvisiously changing the behavior) into:

if (x)
{
 ...
}
else 
{
    if(y)
    {
     ...
    }
    else
    {
        if(z)
        {
         ...
        }
    }
}

This made me realize (unintended) that this is actually what a else if does by syntax and semantical rules of C.

So is there even an statement like else if() existing or is it just an abuse of semantic that results in this usefull but (lets call it so for this purpose) obfuscation originated wording that breaks any formating rules and just serves as human readable?

解决方案

As per the C11, chapter §6.8.4, selection statements, the available statement blocks are

if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement

and, regarding the else part,

An else is associated with the lexically nearest preceding if that is allowed by the syntax.

So, there is no else if construct, exists as per standard C.

Obviously, an if (or if...else) block can exist as the statement in else block (nested if...else, we may say).

The choice of indentation , is left to the user.


Note:

Just to add, there is no reason for a separate else if construct to exist, the functionality can be achieved by nesting. See this wiki article. Relevant quote

The often-encountered else if in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent "if then else" statements combined with a particular source code layout. However, this also means that a distinct else-if construct is not really needed in these languages.

这篇关于是否存在else if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆