The difference is that in the first code snippet, only 1 "std::cout" statement is executed if "the_beans_are_hot." This is the way conditional statements behave absent braces. In the second code snippet, both "std::cout" statements are executed.
However, omitting the braces during a function, class, switch, namespace, and enumeration definition is an error.
The program wouldn't stop, no. Let's walk through it:
First, "the_beans_are_hot" is tested. If the test evaluates to true, the first "std::cout" statement is executed. If the test evaluates to false, the first "std::cout" is ignored, but the second "std::cout" is not. Why? If an "if" statement is not followed by a set of braces, and the test evaluates to true, only 1 statement can be executed, and the statement is the one that follows the "if." Here's how it would work:
1 2 3 4 5 6 7 8 9
if(the_beans_are_hot == true)
std::cout << "Something..."; // This is executed.
// If the above test is true, the "std::cout" statement is executed.
// If not, the statement is skipped.
// The program continues from this point. The subsequent statement
// is not related to the above "if" statement. Therefore, this statement
// is not conditional, and must be executed.
std::cout << "Something else...";
So, I don't think I would actually write code this way because it kindof defeats the purpose of having if/else. If I put the brackets this way, and both x and y were true (for example x=5, y=20) then the output should be
Well, your braces are mixed up in your code, so it's difficult to determine the output. It seems you're struggling with control structures more than I initially expected. I recommend going here: http://www.cplusplus.com/doc/tutorial/control/
Well, it's relatively simple to roll your own program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main( )
std::cout << "Please, enter a positive number: ";
std::cin >> iInput;
if(iInput < 0)
std::cout << "I said enter a POSITIVE number.\n";
std::cout << "Examples: 1, 5, 1000, etcetera.\n";
std::cout << "Well done - at least you have a brain.\n";
@cheshirecat: You asked for a program that demonstrates the use of the "if" statement forms we've been discussing. If you don't understand the code, then it's clear that you don't understand very basic I/O operations. If this is the case, then you shouldn't be learning control structures just yet.
As Chervil said, have you even tried compiling the program?
Well thanks for writing one, but I just wanted to know about moving the brackets around, and how that would affect output. I didn't try compiling the program. I am still looking for a decent free one to download. I did manage to find codepad.org. I Threw the code in there, and I've just been moving brackets around to see how it affects output.
An if or an else will only do 1 thing. So in your code, the "#####" line is the one thing associated with the else. Because your condition in line 6 is met, line 8 is executed, and the else action in line 12 is skipped.
Because only 1 thing can be associated with the else, nothing after line 12 is part of the else action. So, as the program executes, it checks line 6. It sees that the condition is met and executes line 8. It then skips the rest of the if/else construct by going past the end of the else action, and resumes execution at line 13.
If you want more that one action associated with an if or else clause, you must use a compound statement. By wrapping multiple lines within curly braces, you create a new "one thing" that can execute in an if or else clause. So, if you wrapped lines 12 through 14 within curly braces, only line 8 would print.
Many coding standards require the use of curly braces in all if and else clauses, even if the clause only contains 1 statement. This prevents the confusion you are experiencing and helps clarify what is happening in each clause. (It also makes it easier to quickly add debugging code, if necessary, to a clause without breaking the logic of the program, but that's a more advanced thing.)