Curly Brackets

Pages: 12
You can / should use indentation and blank lines to indicate to the human reader the way the code is logically structured.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
    int x = 15;
    int y = 5;  
    
    if (y<10)
    {
        if (x>5)
            cout<<"@@@@@"<<endl;
        else
            cout<<"#####"<<endl;

        cout<<"$$$$$"<<endl;
        cout<<"&&&&&"<<endl <<endl;
    }
}


Some languages, such as Python, make use of indentation not just for the human reader, but to actually control the program logic too, so proper indentation is a good habit to acquire.

(There may be several different styles of indention, and that's not a topic I want to get into here).
Thanks Doug,

That was very helpful.
And thanks chervil, it's good to know why I should always use proper indent.
Would it be possible to get all of the 'else' output if y is false? I'm trying to move the curly brackets around to get that to happen. I know there are more efficient ways to do that. I just want to know if its possible. I've tried every variation I can think of for brackets - adding them, substracting them, moving them. Looks like if y is false, then the program just can't work beyond that. Is that right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
        int x = 15;
        int y = 12;  
        if(y<10)
    {
        if(x>5)
       {
        cout<<"@@@@@"<<endl;
       }
       else
       {
            cout<<"#####"<<endl;
            cout<<"$$$$$"<<endl;
            cout<<"&&&&&"<<endl <<endl;
       }
    }
}
Last edited on
Your current code does not have an else statement for the y condition. Just move it as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
    int x = 15;
    int y = 12;  
    if(y<10)
    {
        if(x>5)
       {
        cout<<"@@@@@"<<endl;
       }
    }
    else
    {
         cout<<"#####"<<endl;
         cout<<"$$$$$"<<endl;
         cout<<"&&&&&"<<endl <<endl;
    }
}
Oh I see, just making the else attached to the first if. Thanks
Topic archived. No new replies allowed.
Pages: 12