如果语句和花括号..有/无不同结果 [英] If Statements And Braces.. Different Result With/Without

查看:101
本文介绍了如果语句和花括号..有/无不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在学习C ++,在使用其中一个教程时,我遇到了奇怪的效果,但是在发生这种情况时我并不太了解。.

Alright, so I'm in the process of learning C++, and I've hit a strange effect while working with one of the tutorials, and I don't quite get while it's happening..

对于本教程(从提供的示例中复制),我在类模板专门化中编写了一个函数,该函数检查char是否为小写,然后将其变为大写:

For the tutorial (copied from the example offered), I have written a function within a class template specialization that checks if the char is lowercase, and then makes it uppercase:

char uppercase ()
{
    //checks to see if "element"(char) is a lower-case letter between 'a' and 'z'
    if ((element >= 'a') && (element <= 'z'))
    {
        //changes value of "element" to be element + (value of A - Value of a)[-32]
        element += 'A' - 'a'; //element = element + -32
        return element;
    };
};

现在,奇怪的是..即使char已经是大写,该代码仍然会被触发。因此,如果我使用值 j调用该函数,则显然可以得到预期的 J。但是,如果我使用值 J进行调用,则该函数仍会通过..因此,该函数不会更改任何内容,而是返回 *。

Now, strangely.. even if the char is already uppercase, this code still gets triggered. So if I call the function with a value of 'j', I obviously get 'J', as intended. However, if I call with a value of 'J', the function still goes through.. so instead of the function not changing anything, it instead returns '*'.

现在,有趣的是..如果我从if语句中删除括号,它将按预期工作。因此,使用

Now, interestingly.. if I remove the brackets from the if statement, it works as intended. So, with

char uppercase ()
{
    //checks to see if "element"(char) is a lower-case letter between 'a' and 'z'
    if ((element >= 'a') && (element <= 'z'))
        //changes value of "element" to be element + (value of A - Value of a)[-32]
        element += 'A' - 'a'; //element = element + -32
        return element;
};

它完全按预期工作。调用值 J的函数只会返回 J,因为不满足条件,因此不会执行任何代码。现在,我知道在if语句中花括号是可选的,但本教程说,不止一个语句需要花括号。

it works completely as intended. Calling the function with the value 'J' just returns 'J', because the conditions weren't met, and thus no code was executed. Now, I know that braces are optional with if statements, but the tutorial says that braces are required for more than one statement.

还值得注意的是在

所以,基本上,..我是否丢失了有关花括号的内容,或者这只是编译器(Visual Studio 2012)的问题吗?

So, basically.. am I missing something about braces, or is this just an issue with the compiler (Visual Studio 2012)?

推荐答案

这是每个设计的如果仅接受一条语句,则大括号将一条语句变成块。

That's per design, the if takes one statement only, the braces make a block one statement.

如果要通过缩进作用域,请使用Python。

If you want scope by indentation use Python.

代码的问题是,当 if 语句不匹配时,您不返回任何内容,这将导致未定义的行为。您的编译器可能会警告您。不要忽略编译器警告。

The problem with your code is that you do not return anything when the if statement does not match, which results in undefined behavior. Your compiler probably gives you a warning about that. Don't ignore compiler warnings.

第二个代码块实际上是您想要的,只更改变量 element 当您的 if 匹配但总是返回变量 element 时。

The second block of code is actually what you want, only change the variable element when your if matches, but always return the variable element.

这篇关于如果语句和花括号..有/无不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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