If Statements And Braces.. Different Result With/Without

后端 未结 3 913
小鲜肉
小鲜肉 2021-01-26 06:40

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..

F

3条回答
  •  忘掉有多难
    2021-01-26 07:06

    In the first case, you are not executing a return statement if the condition isn't met. Presumably the function is returning garbage.

    In the second case, because there are no braces, only one line is dependent on the condition. The return statement is executed regardless. In other words, your second example is equivalent to:

    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;
    };
    

提交回复
热议问题