Curly Brackets

Pages: 12
Sep 22, 2012 at 5:45pm
If I write this program in two different ways - one without the brackets, and one with, would it function differently?
Like for A, do both if statements have to be true for line 5 to occur?


2 (a)
3 if ( x < 10 )
4 if ( y > 10 )
5 cout << "*****" << endl;
6 else
7 cout << "#####" << endl;
8 cout << "$$$$$" << endl;
9
10 (b)
11 if ( x < 10 )
12 {
13 if ( y > 10 )
14 cout << "*****" << endl;
15 }
16 else
17 {
18 cout << "#####" << endl;
19 cout << "$$$$$" << endl;
20 }
Sep 22, 2012 at 5:50pm
...
Last edited on Sep 22, 2012 at 6:11pm
Sep 22, 2012 at 5:51pm
closed account (zb0S216C)
cheshirecat wrote:
"If I write this program in two different ways - one without the brackets, and one with, would it function differently?"

Very differently. Consider this:

1
2
3
if(the_beans_are_hot)
    std::cout << "Something...";
    std::cout << "Something else...";

...and this:

1
2
3
4
5
if(the_beans_are_hot)
{
    std::cout << "Something...";
    std::cout << "Something else...";
}

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.

Wazzak
Last edited on Sep 22, 2012 at 5:52pm
Sep 22, 2012 at 8:58pm
So for your first example, the program would just stop at line 2?
Sep 22, 2012 at 10:03pm
Am I right in what I said above? It would stop at line 2, in the first example that framework gave?
Sep 22, 2012 at 10:10pm
closed account (zb0S216C)
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...";

Wazzak
Sep 23, 2012 at 12:10am
So if there are brackets, then both would be done?
Sep 23, 2012 at 9:59am
closed account (zb0S216C)
Yes.

Wazzak
Sep 23, 2012 at 3:14pm
1
2
3
4
5
6
7
8
9
10
11
12
if(x<10)
{
   if(y>10)
   {
        cout<<"*****"<<endl;
   else
        cout<<"#####"<<endl;
    }
   {   	
        cout<<"$$$$$"<<endl;
   }
}


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
*****
#####

Is that correct?
Last edited on Sep 23, 2012 at 3:15pm
Sep 23, 2012 at 3:19pm
closed account (zb0S216C)
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/

Wazzak
Sep 23, 2012 at 3:35pm
Well, I know it's supposed to be more like this

1
2
3
4
5
6
7
8
9
10
11
12
if(x<10)
{
   if(y>10)
   {
        cout<<"*****"<<endl;
   }
   else
   {
        cout<<"#####"<<endl;
       	cout<<"$$$$$"<<endl;
   }
}


I just wanted to see what would happen. Are there any good free programs I could run this through to see the results of various code changes?
Last edited on Sep 23, 2012 at 3:35pm
Sep 23, 2012 at 7:00pm
closed account (zb0S216C)
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: ";

    int iInput(0);
    std::cin >> iInput;

    if(iInput < 0)
    {
        std::cout << "I said enter a POSITIVE number.\n";
        std::cout << "Examples: 1, 5, 1000, etcetera.\n";
    }

    else
        std::cout << "Well done - at least you have a brain.\n";

    return(0);
}

Wazzak
Last edited on Sep 23, 2012 at 9:00pm
Sep 23, 2012 at 8:22pm
What do you mean? I don't understand any of that. How does that answer any of my questions?
Last edited on Sep 23, 2012 at 8:23pm
Sep 23, 2012 at 8:48pm
Feed that code through your compiler and see how it runs.
You do have a compiler, don't you?
Sep 23, 2012 at 9:08pm
closed account (zb0S216C)
@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?

Wazzak
Sep 24, 2012 at 1:03pm
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.
Sep 24, 2012 at 1:15pm
A code inside braces is named as compound statement that is it is a C/C++ sttatement. So where a C/C++ statement is used you can substitute it for a compound statement.

if-else (another C/C++ statement) statement is defined the following way

if ( condition ) statement1;
else statement2;

In this definition you can substitute statement1 and/or statement2 for a compound statement because it is a C/C++ statement.

So you can rewrite this if-else statement the following ways

if ( condition ) compound_statement1;
else statement2;

if ( condition ) statement1;
else compound_statement2;

if ( condition ) compound_statement1;
else compound_statement2;

Also empty statement is also a C/C++ statement. So you can write if-else even the following ways

if ( condition ) ;
else statement2; // or compound_statement2;

if ( condition ) statement1; // or compound statement1
else ;


And even the following way though it has no a great sense

if ( condition ) ;
else ;




Where compound_statement1 and compound_statement2 are some code enclosed in braces.

Last edited on Sep 24, 2012 at 1:19pm
Sep 24, 2012 at 2:11pm
I'm not sure what you mean by compound statement.

Would anyone be able to explain to me why, if I write it like this, ##### is skipped for output? The output for this is
@@@@@
$$$$$
&&&&&

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
       int x = 15;
       int y = 5;  
       if(y<10)
    {
       if(x>5)
       
            cout<<"@@@@@"<<endl;
       
       else
       
            cout<<"#####"<<endl;
            cout<<"$$$$$"<<endl;
            cout<<"&&&&&"<<endl <<endl;
       
    }
}
Sep 24, 2012 at 2:13pm
I will cite myself

Where compound_statement1 and compound_statement2 are some code enclosed in braces.
Sep 24, 2012 at 2:52pm
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.)
Last edited on Sep 24, 2012 at 2:54pm
Pages: 12